3 回答
TA贡献2036条经验 获得超8个赞
您可以编写一个标记属性:
public class SkipMyGlobalActionFilterAttribute : Attribute
{
}
然后在全局操作过滤器中测试操作中是否存在此标记:
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
}
}
然后,如果要从全局过滤器中排除某些操作,只需用marker属性装饰它即可:
[SkipMyGlobalActionFilter]
public ActionResult Index()
{
return View();
}
TA贡献1829条经验 获得超6个赞
虽然,达林·迪米特洛夫(Darin Dimitrov)接受的答案很好,而且效果很好,但是对我来说,这里建立的最简单,最有效的答案。
您只需要在逻辑开始之前向属性添加一个布尔属性并对其进行检查:
public class DataAccessAttribute: ActionFilterAttribute
{
public bool Disable { get; set; }
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (Disable) return;
// Your original logic for your 95% actions goes here.
}
}
然后按照5%的操作使用,如下所示:
[DataAccessAttribute(Disable=true)]
public ActionResult Index()
{
return View();
}
- 3 回答
- 0 关注
- 421 浏览
添加回答
举报