在中间件中获取 UrlHelper asp.net mvc core 2.0
Get UrlHelper in middleware asp.net mvc core 2.0
如何获取中间件中的UrlHelper。
我尝试如下但 actionContextAccessor.ActionContext
return null.
public void ConfigureServices(IServiceCollection services)
{
services.AddSession();
services
.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie();
services.AddMvc();
services.AddSingleton<IActionContextAccessor, ActionContextAccessor>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseSession();
if (env.IsDevelopment())
{
app.UseBrowserLink();
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseAuthentication();
app.Use(async (context, next) =>
{
var urlHelperFactory = context.RequestServices.GetService<IUrlHelperFactory>();
var actionContextAccessor = context.RequestServices.GetService<IActionContextAccessor>();
var urlHelper = urlHelperFactory.GetUrlHelper(actionContextAccessor.ActionContext);
await next();
// Do logging or other work that doesn't write to the Response.
});
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
您可以尝试将其添加到 ConfigureServices(IServiceCollection services)
上的服务容器中。您可以像下面这样操作:
public IServiceProvider ConfigureServices(IServiceCollection services) {
//Service injection and setup
services.AddSingleton<IActionContextAccessor, ActionContextAccessor>();
services.AddScoped<IUrlHelper>(factory =>
{
var actionContext = factory.GetService<IActionContextAccessor>()
.ActionContext;
return new UrlHelper(actionContext);
});
//....
// Build the intermediate service provider then return it
return services.BuildServiceProvider();
}
然后你可以修改你的 Configure()
方法来接收 IServiceProvider
,你可以通过执行以下操作获得 UrlHelper
的实例。
public void Configure(IApplicationBuilder app, IServiceProvider serviceProvider) {
//Code removed for brevity
var urlHelper = serviceProvider.GetService<IUrlHelper>(); <-- helper instance
app.UseMvc();
}
注意:将其放在services.Mvc()
之后
您的应用中有 2 个管道。您要连接的 ASP.NET 核心管道。以及由 UseMvc 设置的 ASP.NET MVC 管道。 ActionContext 是一个 MVC 概念,这就是它在 ASP.NET 核心管道中不可用的原因。要连接到 MVC 管道,您可以使用过滤器:https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/filters
编辑:修复了link文章
如何获取中间件中的UrlHelper。
我尝试如下但 actionContextAccessor.ActionContext
return null.
public void ConfigureServices(IServiceCollection services)
{
services.AddSession();
services
.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie();
services.AddMvc();
services.AddSingleton<IActionContextAccessor, ActionContextAccessor>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseSession();
if (env.IsDevelopment())
{
app.UseBrowserLink();
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseAuthentication();
app.Use(async (context, next) =>
{
var urlHelperFactory = context.RequestServices.GetService<IUrlHelperFactory>();
var actionContextAccessor = context.RequestServices.GetService<IActionContextAccessor>();
var urlHelper = urlHelperFactory.GetUrlHelper(actionContextAccessor.ActionContext);
await next();
// Do logging or other work that doesn't write to the Response.
});
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
您可以尝试将其添加到 ConfigureServices(IServiceCollection services)
上的服务容器中。您可以像下面这样操作:
public IServiceProvider ConfigureServices(IServiceCollection services) {
//Service injection and setup
services.AddSingleton<IActionContextAccessor, ActionContextAccessor>();
services.AddScoped<IUrlHelper>(factory =>
{
var actionContext = factory.GetService<IActionContextAccessor>()
.ActionContext;
return new UrlHelper(actionContext);
});
//....
// Build the intermediate service provider then return it
return services.BuildServiceProvider();
}
然后你可以修改你的 Configure()
方法来接收 IServiceProvider
,你可以通过执行以下操作获得 UrlHelper
的实例。
public void Configure(IApplicationBuilder app, IServiceProvider serviceProvider) {
//Code removed for brevity
var urlHelper = serviceProvider.GetService<IUrlHelper>(); <-- helper instance
app.UseMvc();
}
注意:将其放在services.Mvc()
您的应用中有 2 个管道。您要连接的 ASP.NET 核心管道。以及由 UseMvc 设置的 ASP.NET MVC 管道。 ActionContext 是一个 MVC 概念,这就是它在 ASP.NET 核心管道中不可用的原因。要连接到 MVC 管道,您可以使用过滤器:https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/filters
编辑:修复了link文章