欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

asp.net mvc core管道及攔截器的理解

 更新時間:2020年05月17日 10:21:21   作者:暖如太陽  
這篇文章主要給大家介紹了關于asp.net mvc core管道及攔截器的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用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ù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論