.Net Core 过滤器详解
.Net MVC 常用的4种过滤器:
Action过滤器(IActionFilter):行为过滤器
Result过滤器(IResultFilter):结果过滤器
Exception过滤器(IExceptionFilter):异常过滤器
Authorization过滤器(IAuthorizationFilter): 权限效验过滤器
Action过滤器:在Action执行之前和执行之后调用。
Result过滤器:在结果之前和之后调用。
Exception过滤器:在发生异常时调用。
Authorization过滤器:身份验证时调用。
摘要:
An abstract filter that asynchronously surrounds execution of the action and
the action result. Subclasses should override Microsoft.AspNetCore.Mvc.Filters.ActionFilterAttribute.OnActionExecuting(Microsoft.AspNetCore.Mvc.Filters.ActionExecutingContext),Microsoft.AspNetCore.Mvc.Filters.ActionFilterAttribute.OnActionExecuted(Microsoft.AspNetCore.Mvc.Filters.ActionExecutedContext)orMicrosoft.AspNetCore.Mvc.Filters.ActionFilterAttribute.OnActionExecutionAsync(Microsoft.AspNetCore.Mvc.Filters.ActionExecutingContext,Microsoft.AspNetCore.Mvc.Filters.ActionExecutionDelegate) but not Microsoft.AspNetCore.Mvc.Filters.ActionFilterAttribute.OnActionExecutionAsync(Microsoft.AspNetCore.Mvc.Filters.ActionExecutingContext,Microsoft.AspNetCore.Mvc.Filters.ActionExecutionDelegate) and either of the other two. Similarly subclasses should override Microsoft.AspNetCore.Mvc.Filters.ActionFilterAttribute.OnResultExecuting(Microsoft.AspNetCore.Mvc.Filters.ResultExecutingContext),Microsoft.AspNetCore.Mvc.Filters.ActionFilterAttribute.OnResultExecuted(Microsoft.AspNetCore.Mvc.Filters.ResultExecutedContext) or Microsoft.AspNetCore.Mvc.Filters.ActionFilterAttribute.OnResultExecutionAsync(Microsoft.AspNetCore.Mvc.Filters.ResultExecutingContext,Microsoft.AspNetCore.Mvc.Filters.ResultExecutionDelegate) but not Microsoft.AspNetCore.Mvc.Filters.ActionFilterAttribute.OnResultExecutionAsync(Microsoft.AspNetCore.Mvc.Filters.ResultExecutingContext,Microsoft.AspNetCore.Mvc.Filters.ResultExecutionDelegate) and either of the other two.
过滤器:
这里主要说一下action过滤器,顾名思义,可以进行action行为前后的一些处理,通常用来记录日志,或者特殊处理。
使用方法:
创建一个过滤器类,继承抽象类ActionFilterAttribute(IActionFilter、IResultFilter这俩货都在抽象类中),异常过滤器接口IExceptionFilter
override重写其虚方法:执行节点如下
OnActionExecuted: 进入action后,返回result前
OnActionExecuting: 进入action前
OnActionExecutionAsync: 进入acion前(异步)
OnResultExecuted: 返回result后
OnResultExecuting: 返回result前
OnResultExecutionAsync: 返回result前(异步)
OnException:抛异常进入
下面上代码,具体实现看代码吧。
using Microsoft.AspNetCore.Mvc.Filters;
using System;
using System.Threading.Tasks;
namespace Filter
{
public class ActionFilter: ActionFilterAttribute, IExceptionFilter
{
public override void OnActionExecuted(ActionExecutedContext context)
{
Console.WriteLine("进入action之后,返回result前");
}
public override void OnActionExecuting(ActionExecutingContext context)
{
Console.WriteLine("进入action之前");
}
public override void OnResultExecuted(ResultExecutedContext context)
{
Console.WriteLine("返回result之后");
}
public override void OnResultExecuting(ResultExecutingContext context)
{
Console.WriteLine("返回result之前");
}
public void OnException(ExceptionContext context)
{
Console.WriteLine("报错啦!");
throw new NotImplementedException();
}
/*
public override Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
{
Console.WriteLine("进入action之前(异步)");
return Task.CompletedTask;
}
public override Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next)
{
Console.WriteLine("返回result之前(异步)");
return Task.CompletedTask;
}
*/
}
}
最后只需要在需要的地方,标注上该特性就可以直接使用了。
结果: