C# 在中间件asp.net mvc core 2.0中获取UrlHelper
如何在中间件中获得UrlHelper。C# 在中间件asp.net mvc core 2.0中获取UrlHelper,c#,dependency-injection,middleware,asp.net-core-mvc-2.0,C#,Dependency Injection,Middleware,Asp.net Core Mvc 2.0,如何在中间件中获得UrlHelper。 我尝试如下操作,但actionContextAccessor.ActionContext返回null public void ConfigureServices(IServiceCollection services) { services.AddSession(); services .AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
我尝试如下操作,但
actionContextAccessor.ActionContext
返回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?}");
});
}
public void配置服务(IServiceCollection服务)
{
services.AddSession();
服务
.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie();
services.AddMvc();
services.AddSingleton();
}
//此方法由运行时调用。使用此方法配置HTTP请求管道。
公共无效配置(IApplicationBuilder应用程序,IHostingEnvironment环境)
{
app.UseSession();
if(env.IsDevelopment())
{
app.UseBrowserLink();
app.UseDeveloperExceptionPage();
}
其他的
{
app.UseExceptionHandler(“/Home/Error”);
}
app.UseStaticFiles();
app.UseAuthentication();
应用程序使用(异步(上下文,下一步)=>
{
var urlHelperFactory=context.RequestServices.GetService();
var actionContextAccessor=context.RequestServices.GetService();
var urlHelper=urlHelperFactory.GetUrlHelper(actionContextAccessor.ActionContext);
等待下一个();
//执行不写入响应的日志记录或其他工作。
});
app.UseMvc(路由=>
{
routes.MapRoute(
名称:“默认”,
模板:“{controller=Home}/{action=Index}/{id?}”);
});
}
您可以尝试将其添加到配置服务(iSeries收集服务)
上的服务容器中。您可以按如下方式执行此操作:
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();
}
注意:将其放在
services.Mvc()之后
应用程序中有两条管道。正在连接的ASP.NET核心管道。以及UseMvc建立的ASP.NETMVC管道。ActionContext是一个MVC概念,这就是为什么它在ASP.NET核心管道中不可用的原因。要连接到MVC管道,可以使用过滤器:
编辑:修复了文章的链接那么,我如何在中间件中获得UrlHelper?我不知道如何使用它,在添加
服务之后。AddScoped(…)
actionContextAccessor.ActionContext
仍然返回null,在行返回新的UrlHelper(ActionContext)时抛出异常代码>你提到的链接是旧的mvc 3,而不是mvc,现在我可以在全局范围内添加一个过滤器。
public void Configure(IApplicationBuilder app, IServiceProvider serviceProvider) {
//Code removed for brevity
var urlHelper = serviceProvider.GetService<IUrlHelper>(); <-- helper instance
app.UseMvc();
}