asp.net mvc core 管道以及拦截器初了解

前端之家收集整理的这篇文章主要介绍了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( ResourceFilter());
        config.Filters.Add(new AsyncResourceFilter());
        config.Filters.Add( ActionFilter());
        config.Filters.Add(new AsyncActionFilter());
        config.Filters.Add( ResultFilter());
        config.Filters.Add(new AsyncResultFilter());
        config.Filters.Add( 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.
 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 Fawait context.Response.WriteAsync(hello world);
        Console.WriteLine(execute G);
    });
    app.UseSession();
    app.UseEndpoints(endpoints =>execute A);
        endpoints.MapControllerRoute(
            name: default,pattern: {controller=Home}/{action=Index}/{id?}execute B);
    });
}

执行结果:

 

 

 

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

看完中间件执行顺序,下面我们来了解下mvc拦截器的使用与执行顺序。

根据mvc filter管道执行顺序,我们分别来看下用法

1)AuthorizationFilter:该拦截器是优先级最高的,当请求进入mvc后,首先会被AuthorizationFilter验证是否有权限访问,无权限则跳出。

同步用法

class AuthorizationFilter: IAuthorizationFilter
{
     OnAuthorization(AuthorizationFilterContext context)
    {
        context.HttpContext.Response.WriteAsync(authorization filter \r);
    }
}

异步用法

 AsyncAuthorizationFilter: IAsyncAuthorizationFilter
{
    async Task OnAuthorizationAsync(AuthorizationFilterContext context)
    {
        await context.HttpContext.Response.WriteAsync($async authorization filter in \r);
    }
}

2)ResourceFilter:该拦截器是作为第二道拦截器,

OnResourceExecuting在模型绑定之前运行代码。OnResourceExecuted在管道的其余阶段完成之后运行代码

同步用法

 ResourceFilter: IResourceFilter
{
     OnResourceExecuting(ResourceExecutingContext context)
    {
        context.HttpContext.Response.WriteAsync($resource executing\r);
    }
     OnResourceExecuted(ResourceExecutedContext context)
    {
        context.HttpContext.Response.WriteAsync($resource executed \r AsyncResourceFilter: IAsyncResourceFilter
{
     Task OnResourceExecutionAsync(ResourceExecutingContext context,ResourceExecutionDelegate next)
    {
         async resource filter in. \r\nawait next();
        async resource filter out. \r\n);
    }
}

3)ActionFilter:调用操作方法之前和之后立即运行代码可以更改传递到操作中的参数;可以更改从操作返回的结果。

同步用法

 ActionFilter: IActionFilter
{
     OnActionExecuting(ActionExecutingContext context)
    {
        context.HttpContext.Response.WriteAsync($action executing \r);
    }

     OnActionExecuted(ActionExecutedContext context)
    {
        context.HttpContext.Response.WriteAsync($action executed . \r Task OnActionExecutionAsync(ActionExecutingContext context,ActionExecutionDelegate next)
{
    async action execution in. \r\n);
     next();
    async action execution out. \r\n);
}

4)OnException:在向响应正文写入任何内容之前,对声明处理的异常应用变量策略

同步用法

 ExceptionFilter: IExceptionFilter
{
     OnException(ExceptionContext context)
    {
        context.HttpContext.Response.WriteAsync($exception \r AsyncExceptionFilter: IAsyncExceptionFilter
{
    public Task OnExceptionAsync(ExceptionContext context)
    {
        context.HttpContext.Response.WriteAsync($exception async \rreturn Task.CompletedTask;
    }
}

5)ResultFilter:在执行操作结果之前和之后立即运行代码;仅当操作方法成功执行时,其才会运行。 可以设置格式化返回结果: 

同步操作:

 ResultFilter: IResultFilter
{
     OnResultExecuting(ResultExecutingContext context)
    {
        context.HttpContext.Response.WriteAsync($result executing\r OnResultExecuted(ResultExecutedContext context)
    {
        context.HttpContext.Response.WriteAsync($result executed \r AsyncResultFilter: IAsyncResultFilter
{
     Task OnResultExecutionAsync(ResultExecutingContext context,ResultExecutionDelegate next)
    {
        result execution async in \rresult execution async out. \r);
    }
}

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

 

 定义一个异常看下结果:

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

@H_561_404@

 

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

参考文档:ASP.NET Core 中的筛选器

原创,转载注明出处。

 

 

 

 

 

原文链接:/aspnet/994761.html

猜你在找的asp.Net相关文章