services.AddMvc(options =>
{
options.Filters.Add(typeof(MyActionFilterAttribute));
}).SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
MyActionFilterAttribute的实现
public class MyActionFilterAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext context)
{
var controllerActionDescriptor = context.ActionDescriptor as ControllerActionDescriptor;
if (controllerActionDescriptor != null)
{
var isDefined = controllerActionDescriptor.ControllerTypeInfo.GetCustomAttributes(inherit: true)
.Any(a => a.GetType().Equals(typeof(NoActionFilterAttribute)));
if (!isDefined)
{
//业务逻辑
base.OnActionExecuting(context);
}
}
}
}
NoActionFilterAttribute
public class NoActionFilterAttribute : Attribute
{
}
在不需要验证的Controller上打上NoActionFilter特性即可
[NoActionFilter]
public class TestController : Controller
{
}