您好,登錄后才能下訂單哦!
本篇內容介紹了“如何理解asp.net mvc core管道及攔截器”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
今天來看一下asp.net core的執行管道。先看下官方說明:
從上圖可以拋光,asp.net core的執行順序是,當收到一個請求后,request請求會先經過已注冊的中間件,然后會進入到mvc的攔截器管道:
進入mvc管道后,根據以上順序執行過濾校正。
OK,根據以上說明下面我們新建一個MVC的演示,將執行方式切換為控臺運行:
// 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"); }); }
執行結果:
不多做解釋,從從這里我們可以拋光符合官方說明文檔。
看完中間件執行順序,下面我們來了解下mvc攔截器的使用與執行順序。
根據mvc filter管道執行順序,我們分別來看下用法:
1)AuthorizationFilter:該攔截器是優先級最高的,當請求進入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:在調用操作方法之前和之后立即運行代碼;可以更改傳遞到操作中的參數;可以更改從操作返回的結果。
同步用法:
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:在向響應正文寫入任何內容之前,對聲明處理的異常應用變量策略。
同步用法:
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:在執行操作結果之前和之后立即運行代碼;僅當操作方法成功執行時,其才會運行。 可以設置格式化返回結果:
同步操作:
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"); } }
注冊方式我們就是用分區注冊,已經在上面說明,不再多做表述,下面我們看下運行情況(頁面輸出):
定義一個異常看下結果:
public IActionResult Privacy() { throw new Exception("error"); }
ok,目標達成,不多說了,下次再看攔截器具體實現。
總結
“如何理解asp.net mvc core管道及攔截器”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。