asp.net mvc core管道及攔截器的理解
今天來看一下asp.net core的執(zhí)行管道。先看下官方說明:
從上圖可以拋光,asp.net core的執(zhí)行順序是,當收到一個請求后,request請求會先經(jīng)過已注冊的中間件,然后會進入到mvc的攔截器管道:
進入mvc管道后,根據(jù)以上順序執(zhí)行過濾校正。
OK,根據(jù)以上說明下面我們新建一個MVC的演示,將執(zhí)行方式切換為控臺運行:
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddControllersWithViews(config=> { Console.WriteLine("execute C"); //config.Filters.Add(new AsyncAuthorizationFilter()); config.Filters.Add(new AuthorizationFilter()); config.Filters.Add(new ResourceFilter()); //config.Filters.Add(new AsyncResourceFilter()); config.Filters.Add(new ActionFilter()); //config.Filters.Add(new AsyncActionFilter()); config.Filters.Add(new ResultFilter()); //config.Filters.Add(new AsyncResultFilter()); config.Filters.Add(new ExceptionFilter()); //config.Filters.Add(new AsyncExceptionFilter()); Console.WriteLine("execute D"); }); services.AddSession(config=> { Console.WriteLine("execute E"); }); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Home/Error"); } app.UseStaticFiles(); app.UseRouting(); app.UseAuthorization(); app.Use(async (context, next) => { Console.WriteLine("execute F"); await context.Response.WriteAsync("hello world"); Console.WriteLine("execute G"); }); //app.UseSession(); app.UseEndpoints(endpoints => { Console.WriteLine("execute A"); endpoints.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); Console.WriteLine("execute B"); }); }
執(zhí)行結果:
不多做解釋,從從這里我們可以拋光符合官方說明文檔。
看完中間件執(zhí)行順序,下面我們來了解下mvc攔截器的使用與執(zhí)行順序。
根據(jù)mvc filter管道執(zhí)行順序,我們分別來看下用法:
1)AuthorizationFilter:該攔截器是優(yōu)先級最高的,當請求進入mvc后,首先會被AuthorizationFilter驗證是否有權限訪問,無權限則跳出。
同步用法:
public class AuthorizationFilter: IAuthorizationFilter { public void OnAuthorization(AuthorizationFilterContext context) { context.HttpContext.Response.WriteAsync("authorization filter \r"); } }
異步用法:
public class AsyncAuthorizationFilter: IAsyncAuthorizationFilter { public async Task OnAuthorizationAsync(AuthorizationFilterContext context) { await context.HttpContext.Response.WriteAsync($"async authorization filter in \r"); } }
2)ResourceFilter:該攔截器是作為第二道攔截器,
OnResourceExecuting在模型綁定之前運行代碼。OnResourceExecuted在管道的其余階段完成之后運行代碼。
同步用法:
public class ResourceFilter: IResourceFilter { public void OnResourceExecuting(ResourceExecutingContext context) { context.HttpContext.Response.WriteAsync($"resource executing\r"); } public void OnResourceExecuted(ResourceExecutedContext context) { context.HttpContext.Response.WriteAsync($"resource executed \r"); } }
異步用法:
public class AsyncResourceFilter: IAsyncResourceFilter { public async Task OnResourceExecutionAsync(ResourceExecutingContext context, ResourceExecutionDelegate next) { await context.HttpContext.Response.WriteAsync($" async resource filter in. \r\n"); await next(); await context.HttpContext.Response.WriteAsync($"async resource filter out. \r\n"); } }
3)ActionFilter:在調(diào)用操作方法之前和之后立即運行代碼;可以更改傳遞到操作中的參數(shù);可以更改從操作返回的結果。
同步用法:
public class ActionFilter: IActionFilter { public void OnActionExecuting(ActionExecutingContext context) { context.HttpContext.Response.WriteAsync($"action executing \r"); } public void OnActionExecuted(ActionExecutedContext context) { context.HttpContext.Response.WriteAsync($"action executed . \r"); } }
異步用法:
public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) { await context.HttpContext.Response.WriteAsync($"async action execution in. \r\n"); await next(); await context.HttpContext.Response.WriteAsync($"async action execution out. \r\n"); }
4)OnException:在向響應正文寫入任何內(nèi)容之前,對聲明處理的異常應用變量策略。
同步用法:
public class ExceptionFilter: IExceptionFilter { public void OnException(ExceptionContext context) { context.HttpContext.Response.WriteAsync($"exception \r"); } }
異步用法:
public class AsyncExceptionFilter: IAsyncExceptionFilter { public Task OnExceptionAsync(ExceptionContext context) { context.HttpContext.Response.WriteAsync($"exception async \r"); return Task.CompletedTask; } }
5)ResultFilter:在執(zhí)行操作結果之前和之后立即運行代碼;僅當操作方法成功執(zhí)行時,其才會運行。 可以設置格式化返回結果:
同步操作:
public class ResultFilter: IResultFilter { public void OnResultExecuting(ResultExecutingContext context) { context.HttpContext.Response.WriteAsync($"result executing\r"); } public void OnResultExecuted(ResultExecutedContext context) { context.HttpContext.Response.WriteAsync($"result executed \r"); } }
異步用法:
public class AsyncResultFilter: IAsyncResultFilter { public async Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next) { await context.HttpContext.Response.WriteAsync($"result execution async in \r"); await next(); await context.HttpContext.Response.WriteAsync($"result execution async out. \r"); } }
注冊方式我們就是用分區(qū)注冊,已經(jīng)在上面說明,不再多做表述,下面我們看下運行情況(頁面輸出):
定義一個異??聪陆Y果:
public IActionResult Privacy() { throw new Exception("error"); }
ok,目標達成,不多說了,下次再看攔截器具體實現(xiàn)。
參考文檔:ASP.NET Core 中的篩選器
總結
到此這篇關于asp.net mvc core管道及攔截器的文章就介紹到這了,更多相關asp.net mvc core管道及攔截器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
- ASP.Net Core MVC基礎系列之中間件
- ASP.Net Core MVC基礎系列之服務注冊和管道
- ASP.Net?Core?MVC基礎系列之獲取配置信息
- ASP.Net?Core?MVC基礎系列之項目創(chuàng)建
- ASP.NET Core MVC學習之視圖組件(View Component)
- ASP.NET Core MVC基礎學習之局部視圖(Partial Views)
- ASP.NET Core MVC學習教程之路由(Routing)
- ASP.NET Core MVC/WebApi基礎系列2
- ASP.NET Core MVC/WebApi基礎系列1
- ASP.Net?Core?MVC基礎系列之環(huán)境設置
相關文章
基于asp.net實現(xiàn)圖片在線上傳并在線裁剪功能
本文主要介紹了基于asp.net實現(xiàn)圖片在線上傳并在線裁剪功能的具體事例代碼,具有一定的參考價值。需要的朋友可以參考下2016-12-12Asp.net中的數(shù)據(jù)綁定Eval和Bind應用示例
這篇文章主要介紹了Asp.net中的數(shù)據(jù)綁定Eval和Bind的應用,需要的朋友可以參考下2014-05-05asp.net中button控制先執(zhí)行js再執(zhí)行后臺程序的方法
這篇文章主要介紹了asp.net中button控制先執(zhí)行js再執(zhí)行后臺程序的方法,涉及button控件與js的使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-01-01在ASP.NET?MVC下限制同一個IP地址單位時間間隔內(nèi)的請求次數(shù)的解決方法
有時候,當用戶請求一個Controller下的Action,我們希望,在單位時間間隔內(nèi),比如每秒,每分鐘,每小時,每天,每星期,限制同一個IP地址對某個Action的請求次數(shù),如何做呢?這篇文章主要介紹了在ASP.NET?MVC下限制同一個IP地址單位時間間隔內(nèi)的請求次數(shù),需要的朋友可以參考下2024-01-01