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管道及拦截器的文章就介绍到这了,更多相关asp.net mvc core管道及拦截器内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
您可能感兴趣的文章: