如何有select地禁用ASP.Net MVC中的全局filter

我已经设置了一个全局filter,用于打开和closuresNHibernate会话的所有控制器操作。 95%的这些行动需要一些数据库访问,但是5%不需要。 有没有简单的方法来禁用这5%的全球filter。 我可以倒过来装饰只需要数据库的行动,但这将是更多的工作。

你可以写一个标记属性:

public class SkipMyGlobalActionFilterAttribute : Attribute { } 

然后在你的全局行为filtertesting中对这个标记进行动作的存在:

 public class MyGlobalActionFilter : ActionFilterAttribute { public override void OnActionExecuting(ActionExecutingContext filterContext) { if (filterContext.ActionDescriptor.GetCustomAttributes(typeof(SkipMyGlobalActionFilterAttribute), false).Any()) { return; } // here do whatever you were intending to do } } 

然后如果你想从全局filter中排除一些操作,只需使用marker属性来修饰它:

 [SkipMyGlobalActionFilter] public ActionResult Index() { return View(); } 

你也可以做这个真棒文章中描述的:

排除filter

只需实现一个自定义的ExcludeFilterAttribute ,然后是一个自定义的ExcludeFilterProvider

清洁的解决scheme,为我工作很好!

创build一个自定义筛选提供程 写一个将实现IFilterProvider的类。 这个IFilterProvider接口有一个GetFilters方法,它返回需要执行的Filters。

 public class MyFilterProvider : IFilterProvider { private readonly List<Func<ControllerContext, object>> filterconditions = new List<Func<ControllerContext, object>>(); public void Add(Func<ControllerContext, object> mycondition) { filterconditions.Add(mycondition); } public IEnumerable<Filter> GetFilters(ControllerContext controllerContext, ActionDescriptor actionDescriptor) { return from filtercondition in filterconditions select filtercondition(controllerContext) into ctrlContext where ctrlContext!= null select new Filter(ctrlContext, FilterScope.Global); } } 

================================================== ===========================
在Global.asax.cs中

 public static void RegisterGlobalFilters(GlobalFilterCollection filters) { MyFilterProvider provider = new MyFilterProvider(); provider.Add(d => d.RouteData.Values["action"].ToString() != "SkipFilterAction1 " ? new NHibernateActionFilter() : null); FilterProviders.Providers.Add(provider); } protected void Application_Start() { RegisterGlobalFilters(GlobalFilters.Filters); } 

你可以像这样改变你的filter代码:

  public class NHibernateActionFilter : ActionFilterAttribute { public IEnumerable<string> ActionsToSkip { get; set; } public NHibernateActionFilter(params string[] actionsToSkip) { ActionsToSkip = actionsToSkip; } public override void OnActionExecuting(ActionExecutingContext filterContext) { if (null != ActionsToSkip && ActionsToSkip.Any(a => String.Compare(a, filterContext.ActionDescriptor.ActionName, true) == 0)) { return; } //here you code } } 

并使用它:

 [NHibernateActionFilter(new[] { "SkipFilterAction1 ", "Action2"})] 

那么,我想我得到了它的ASP.NET核心工作。
代码如下:

 public override async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) { // Prepare the audit _parameters = context.ActionArguments; await next(); if (IsExcluded(context)) { return; } var routeData = context.RouteData; var controllerName = (string)routeData.Values["controller"]; var actionName = (string)routeData.Values["action"]; // Log action data var auditEntry = new AuditEntry { ActionName = actionName, EntityType = controllerName, EntityID = GetEntityId(), PerformedAt = DateTime.Now, PersonID = context.HttpContext.Session.GetCurrentUser()?.PersonId.ToString() }; _auditHandler.DbContext.Audits.Add(auditEntry); await _auditHandler.DbContext.SaveChangesAsync(); } private bool IsExcluded(ActionContext context) { var controllerActionDescriptor = (Microsoft.AspNetCore.Mvc.Controllers.ControllerActionDescriptor)context.ActionDescriptor; return controllerActionDescriptor.ControllerTypeInfo.IsDefined(typeof(ExcludeFromAuditing), false) || controllerActionDescriptor.MethodInfo.IsDefined(typeof(ExcludeFromAuditing), false); } 

相关代码在“IsExcluded”方法中。

在另一个问题上提供了最好的答案:

MVC 3 GlobalFilters排除