c# – .NET Core EndRequest中间件
发布时间:2020-12-15 23:35:55 所属栏目:百科 来源:网络整理
导读:我正在构建ASP.NET Core MVC应用程序,我需要像以前在Global.asax中那样拥有EndRequest事件. 我怎么能做到这一点? 解决方法 它就像创建中间件一样简单,并确保它在管道中尽快注册. 例如: public class EndRequestMiddleware{ private readonly RequestDelega
我正在构建ASP.NET Core MVC应用程序,我需要像以前在Global.asax中那样拥有EndRequest事件.
我怎么能做到这一点? 解决方法
它就像创建中间件一样简单,并确保它在管道中尽快注册.
例如: public class EndRequestMiddleware { private readonly RequestDelegate _next; public EndRequestMiddleware(RequestDelegate next) { _next = next; } public async Task Invoke(HttpContext context) { // Do tasks before other middleware here,aka 'BeginRequest' // ... // Let the middleware pipeline run await _next(context); // Do tasks after middleware here,aka 'EndRequest' // ... } } 等待_next(上下文)的调用将导致管道中的所有中间件运行.执行完所有中间件后,将执行await _next(context)调用后的代码.有关中间件的更多信息,请参见ASP.NET Core middleware docs.特别是来自文档的这个图像使中间件执行变得清晰: 现在我们必须将它注册到Startup类中的管道,最好尽快: public void Configure(IApplicationBuilder app) { app.UseMiddleware<EndRequestMiddleware>(); // Register other middelware here such as: app.UseMvc(); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容