Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add HttpContextExtensions for GetActionContextAsync #16557

Merged
merged 11 commits into from
Aug 14, 2024
Merged
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Abstractions;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.AspNetCore.Mvc.Routing;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection;
using OrchardCore.ContentManagement;
using OrchardCore.ContentManagement.Handlers;
using OrchardCore.DisplayManagement;
using OrchardCore.DisplayManagement.Extensions;
using OrchardCore.Media;
using OrchardCore.Mvc.Core.Utilities;
using OrchardCore.Seo.Models;
Expand Down Expand Up @@ -74,7 +71,7 @@ public override Task GetContentItemAspectAsync(ContentItemAspectContext context,

var actionContext = _actionContextAccessor.ActionContext;

actionContext ??= await GetActionContextAsync(_httpContextAccessor.HttpContext);
actionContext ??= await _httpContextAccessor.GetActionContextAsync();
infofromca marked this conversation as resolved.
Show resolved Hide resolved

var urlHelper = _urlHelperFactory.GetUrlHelper(actionContext);

Expand Down Expand Up @@ -182,20 +179,4 @@ public override Task GetContentItemAspectAsync(ContentItemAspectContext context,
aspect.GoogleSchema = part.GoogleSchema;
});
}

internal static async Task<ActionContext> GetActionContextAsync(HttpContext httpContext)
{
var routeData = new RouteData();
routeData.Routers.Add(new RouteCollection());

var actionContext = new ActionContext(httpContext, routeData, new ActionDescriptor());
var filters = httpContext.RequestServices.GetServices<IAsyncViewActionFilter>();

foreach (var filter in filters)
{
await filter.OnActionExecutionAsync(actionContext);
}

return actionContext;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
using OrchardCore.DisplayManagement.Extensions;
using OrchardCore.DisplayManagement.Shapes;
using OrchardCore.Environment.Shell.Scope;
using OrchardCore.Liquid;
Expand Down Expand Up @@ -192,28 +193,12 @@ public static async Task<ViewContext> GetViewContextAsync(LiquidTemplateContext
if (actionContext == null)
{
var httpContext = context.Services.GetRequiredService<IHttpContextAccessor>().HttpContext;
actionContext = await GetActionContextAsync(httpContext);
actionContext = await httpContext.GetActionContextAsync();
infofromca marked this conversation as resolved.
Show resolved Hide resolved
}

return GetViewContext(actionContext);
}

internal static async Task<ActionContext> GetActionContextAsync(HttpContext httpContext)
{
var routeData = new RouteData();
routeData.Routers.Add(new RouteCollection());

var actionContext = new ActionContext(httpContext, routeData, new ActionDescriptor());
var filters = httpContext.RequestServices.GetServices<IAsyncViewActionFilter>();

foreach (var filter in filters)
{
await filter.OnActionExecutionAsync(actionContext);
}

return actionContext;
}

internal static ViewContext GetViewContext(ActionContext actionContext)
{
var services = actionContext.HttpContext.RequestServices;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Abstractions;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection;

namespace OrchardCore.DisplayManagement.Extensions;

public static class HttpContextExtensions
{
public static async Task<ActionContext> GetActionContextAsync(this IHttpContextAccessor httpContextAccessor)
{
var httpContext = httpContextAccessor.HttpContext;
var actionContext = httpContext.RequestServices.GetService<IActionContextAccessor>()?.ActionContext;

if (actionContext != null)
{
return actionContext;
}

return await GetActionContextAsync(httpContext);
MikeAlhayek marked this conversation as resolved.
Show resolved Hide resolved
}
public static async Task<ActionContext> GetActionContextAsync(this HttpContext httpContext)
sebastienros marked this conversation as resolved.
Show resolved Hide resolved
{
var routeData = new RouteData();
routeData.Routers.Add(new RouteCollection());

var actionContext = new ActionContext(httpContext, routeData, new ActionDescriptor());
var filters = httpContext.RequestServices.GetServices<IAsyncViewActionFilter>();

foreach (var filter in filters)
{
await filter.OnActionExecutionAsync(actionContext);
}

return actionContext;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,14 @@
using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Abstractions;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.AspNetCore.Mvc.Razor;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.ViewEngines;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using OrchardCore.DisplayManagement.Descriptors.ShapeTemplateStrategy;
using OrchardCore.DisplayManagement.Extensions;
using OrchardCore.DisplayManagement.Implementation;

namespace OrchardCore.DisplayManagement.Razor;
Expand Down Expand Up @@ -101,7 +98,7 @@ private async Task<IHtmlContent> RenderRazorViewAsync(string viewName, DisplayCo

public async Task<string> RenderViewToStringAsync(string viewName, object model, IViewEngine viewEngine)
{
var actionContext = await GetActionContextAsync();
var actionContext = await _httpContextAccessor.GetActionContextAsync();
infofromca marked this conversation as resolved.
Show resolved Hide resolved
var view = FindView(actionContext, viewName, viewEngine);

using var output = new ZStringWriter();
Expand Down Expand Up @@ -147,30 +144,6 @@ private static IView FindView(ActionContext actionContext, string viewName, IVie
throw new InvalidOperationException(errorMessage);
}

private async Task<ActionContext> GetActionContextAsync()
{
var httpContext = _httpContextAccessor.HttpContext;
var actionContext = httpContext.RequestServices.GetService<IActionContextAccessor>()?.ActionContext;

if (actionContext != null)
{
return actionContext;
}

var routeData = new RouteData();
routeData.Routers.Add(new RouteCollection());

actionContext = new ActionContext(httpContext, routeData, new ActionDescriptor());
var filters = httpContext.RequestServices.GetServices<IAsyncViewActionFilter>();

foreach (var filter in filters)
{
await filter.OnActionExecutionAsync(actionContext);
}

return actionContext;
}

private IHtmlHelper MakeHtmlHelper(ViewContext viewContext, ViewDataDictionary viewData)
{
if (_htmlHelper is IViewContextAware contextAwareHelper)
Expand Down