3 回答

TA貢獻(xiàn)1783條經(jīng)驗(yàn) 獲得超4個(gè)贊
您可以編寫(xiě)一個(gè)標(biāo)記屬性:
public class SkipMyGlobalActionFilterAttribute : Attribute
{
}
然后在全局操作過(guò)濾器中測(cè)試操作中是否存在此標(biāo)記:
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
}
}
然后,如果要從全局過(guò)濾器中排除某些操作,只需用marker屬性裝飾它即可:
[SkipMyGlobalActionFilter]
public ActionResult Index()
{
return View();
}

TA貢獻(xiàn)1869條經(jīng)驗(yàn) 獲得超4個(gè)贊
雖然,達(dá)林·迪米特洛夫(Darin Dimitrov)接受的答案很好,而且效果很好,但是對(duì)我來(lái)說(shuō),這里建立的最簡(jiǎn)單,最有效的答案。
您只需要在邏輯開(kāi)始之前向?qū)傩蕴砑右粋€(gè)布爾屬性并對(duì)其進(jìn)行檢查:
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 關(guān)注
- 500 瀏覽
添加回答
舉報(bào)