欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-03-07 13:44:00
今天来看一下asp.net core的执行管道。先看下官方说明:从上图可以抛光,asp.net core的执行顺序是,当收到一个请求后,request请求会先经过已注册的中间件,然后会进入到mvc的拦...

今天来看一下asp.net core的执行管道。先看下官方说明:

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

 从上图可以抛光,asp.net core的执行顺序是,当收到一个请求后,request请求会先经过已注册的中间件,然后会进入到mvc的拦截器管道:

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

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

执行结果:

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

不多做解释,从从这里我们可以抛光符合官方说明文档。

看完中间件执行顺序,下面我们来了解下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");
 }
}

注册方式我们就是用分区注册,已经在上面说明,不再多做表述,下面我们看下运行情况(页面输出):

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

 定义一个异常看下结果:

public iactionresult privacy()
{
  throw new exception("error");
}

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

ok,目标达成,不多说了,下次再看拦截器具体实现。

参考文档:asp.net core 中的筛选器

总结

到此这篇关于asp.net mvc core管道及拦截器的文章就介绍到这了,更多相关asp.net mvc core管道及拦截器内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!