最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2020年05月17日 10:21:21   作者:暖如太陽(yáng)  
這篇文章主要給大家介紹了關(guān)于asp.net mvc core管道及攔截器的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用asp.net mvc core具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

今天來(lái)看一下asp.net core的執(zhí)行管道。先看下官方說(shuō)明:

 從上圖可以拋光,asp.net core的執(zhí)行順序是,當(dāng)收到一個(gè)請(qǐng)求后,request請(qǐng)求會(huì)先經(jīng)過(guò)已注冊(cè)的中間件,然后會(huì)進(jìn)入到mvc的攔截器管道:

進(jìn)入mvc管道后,根據(jù)以上順序執(zhí)行過(guò)濾校正。

OK,根據(jù)以上說(shuō)明下面我們新建一個(gè)MVC的演示,將執(zhí)行方式切換為控臺(tái)運(yùn)行:

// 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í)行結(jié)果:

不多做解釋,從從這里我們可以拋光符合官方說(shuō)明文檔。

看完中間件執(zhí)行順序,下面我們來(lái)了解下mvc攔截器的使用與執(zhí)行順序。

根據(jù)mvc filter管道執(zhí)行順序,我們分別來(lái)看下用法:

1)AuthorizationFilter:該攔截器是優(yōu)先級(jí)最高的,當(dāng)請(qǐng)求進(jìn)入mvc后,首先會(huì)被AuthorizationFilter驗(yàn)證是否有權(quán)限訪問(wèn),無(wú)權(quán)限則跳出。

同步用法:

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在模型綁定之前運(yùn)行代碼。OnResourceExecuted在管道的其余階段完成之后運(yùn)行代碼。

同步用法:

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)用操作方法之前和之后立即運(yùn)行代碼;可以更改傳遞到操作中的參數(shù);可以更改從操作返回的結(jié)果。

同步用法:

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:在向響應(yīng)正文寫(xiě)入任何內(nèi)容之前,對(duì)聲明處理的異常應(yīng)用變量策略。

同步用法:

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í)行操作結(jié)果之前和之后立即運(yùn)行代碼;僅當(dāng)操作方法成功執(zhí)行時(shí),其才會(huì)運(yùn)行。 可以設(shè)置格式化返回結(jié)果:

同步操作:

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");
 }
}

注冊(cè)方式我們就是用分區(qū)注冊(cè),已經(jīng)在上面說(shuō)明,不再多做表述,下面我們看下運(yùn)行情況(頁(yè)面輸出):

 定義一個(gè)異常看下結(jié)果:

public IActionResult Privacy()
{
  throw new Exception("error");
}

ok,目標(biāo)達(dá)成,不多說(shuō)了,下次再看攔截器具體實(shí)現(xiàn)。

參考文檔:ASP.NET Core 中的篩選器

總結(jié)

到此這篇關(guān)于asp.net mvc core管道及攔截器的文章就介紹到這了,更多相關(guān)asp.net mvc core管道及攔截器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Springboot服務(wù)Docker化自動(dòng)部署的實(shí)現(xiàn)方法

    Springboot服務(wù)Docker化自動(dòng)部署的實(shí)現(xiàn)方法

    這篇文章主要介紹了Springboot服務(wù)Docker化自動(dòng)部署的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • .NET運(yùn)行界面上,實(shí)現(xiàn)隨意拖動(dòng)控件的方法

    .NET運(yùn)行界面上,實(shí)現(xiàn)隨意拖動(dòng)控件的方法

    .NET運(yùn)行界面上,實(shí)現(xiàn)隨意拖動(dòng)控件的方法,需要的朋友可以參考一下
    2013-03-03
  • Asp.net SignalR應(yīng)用并實(shí)現(xiàn)群聊功能

    Asp.net SignalR應(yīng)用并實(shí)現(xiàn)群聊功能

    這篇文章主要為大家分享了Asp.net SignalR應(yīng)用并實(shí)現(xiàn)群聊功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-04-04
  • .NET Core日志配置的方法

    .NET Core日志配置的方法

    熟悉ASP.NET的開(kāi)發(fā)者一定對(duì)web.config文件不陌生,這篇文章主要介紹了.NET Core日志配置的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-08-08
  • asp.net System.Net.Mail 發(fā)送郵件

    asp.net System.Net.Mail 發(fā)送郵件

    一個(gè)師弟發(fā)了段代碼給我,說(shuō)調(diào)試了很久發(fā)送郵件都沒(méi)有成功。自己使用過(guò)程中,也發(fā)現(xiàn)了很多問(wèn)題,但最簡(jiǎn)單的問(wèn)題是“發(fā)件方”地址根本不支持smtp發(fā)送郵件。
    2009-04-04
  • ASP.NET Core 3框架揭秘之 異步線程無(wú)法使用IServiceProvider問(wèn)題

    ASP.NET Core 3框架揭秘之 異步線程無(wú)法使用IServiceProvider問(wèn)題

    這篇文章主要介紹了ASP.NET Core 3框架揭秘之異步線程無(wú)法使用IServiceProvider問(wèn)題,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-12-12
  • .Net Core中使用Autofac替換自帶的DI容器的示例

    .Net Core中使用Autofac替換自帶的DI容器的示例

    Autofac比Core中自帶的DI功能強(qiáng)大的多,比如:屬性注入、基于名稱注入、子容器、自定生存期管理、遲緩初始化,本文就詳細(xì)的來(lái)介紹一下.Net Core Autofac替換DI容器,感興趣的可以了解一下
    2021-06-06
  • Asp.Net分頁(yè)和AspNetPager控件的使用

    Asp.Net分頁(yè)和AspNetPager控件的使用

    在Asp.Net中對(duì)頁(yè)面分頁(yè)的方法很多,可以直接用sql語(yǔ)句分,也可以使用.net提供的PageDataSource類來(lái)分頁(yè),顯示的視圖同樣可以使用第三方控件AspNetPager等來(lái)顯示。
    2011-02-02
  • VS2015下OpenGL庫(kù)配置教程

    VS2015下OpenGL庫(kù)配置教程

    這篇文章主要為大家詳細(xì)介紹了visual studio 2015下OpenGL庫(kù)的配置教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-04-04
  • asp.net 票據(jù)簡(jiǎn)單應(yīng)用

    asp.net 票據(jù)簡(jiǎn)單應(yīng)用

    asp.net票據(jù)應(yīng)用實(shí)例代碼。
    2009-03-03

最新評(píng)論

班玛县| 育儿| 玛曲县| 英山县| 凌云县| 宿州市| 揭阳市| 油尖旺区| 上饶市| 玛纳斯县| 镇坪县| 巴彦县| 黄大仙区| 资阳市| 瑞安市| 沁水县| 马边| 庆阳市| 连山| 丰镇市| 蒙山县| 苍山县| 广宁县| 许昌县| 鄂州市| 郁南县| 玛曲县| 图们市| 会同县| 三都| 平度市| 青海省| 家居| 滨州市| 蒲江县| 玉山县| 秦皇岛市| 工布江达县| 什邡市| 卢龙县| 绥德县|