如何在ASP.Net MVC禁用全局过滤器选择 [英] How to disable a global filter in ASP.Net MVC selectively
问题描述
我已经建立了中,我打开我所有的控制器动作和关闭会话NHibernate的全局筛选器。这些行动的95%需要一些数据库访问,但5%的没有。有没有简单的方法来禁用这些5%这一全球性的过滤器。我可以圆走另一条路,装饰只需要对数据库的操作,但是这将是远远更多的工作。
I have set up a global filter for all my controller actions in which I open and close NHibernate sessions. 95% of these action need some database access, but 5% don't. Is there any easy way to disable this global filter for those 5%. I could go the other way round and decorate only the actions that need the database, but that would be far more work.
推荐答案
您可以编写一个标记属性:
You could write a marker attribute:
public class SkipMyGlobalActionFilterAttribute : Attribute
{
}
,然后在全球行动过滤器测试的动作此标记的presence:
and then in your global action filter test for the presence of this marker on the action:
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
}
}
,然后如果你想排除从全局过滤一些动作简单地用标记属性装饰它:
and then if you want to exclude some action from the global filter simply decorate it with the marker attribute:
[SkipMyGlobalActionFilter]
public ActionResult Index()
{
return View();
}
这篇关于如何在ASP.Net MVC禁用全局过滤器选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!