MVC属性的控制器和动作 [英] MVC Attributes on Controllers and Actions
本文介绍了MVC属性的控制器和动作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法在控制器级别,但不是在具体的行动中添加属性。例如说,如果我在我的控制器10操作,只是这些行动的1不要求我创建了一个特定的属性。
Is there a way to add an Attribute on the Controller level but not on a specific action. For example say if i had 10 Actions in my Controller and just 1 of those Actions does not require a specific attribute I created.
[MyAttribute]
public class MyController : Controller
{
public ActionResult Action1
public ActionResult Action2
[Remove_MyAttribute]
public ActionResult Action3
我可能会提出这个动作到另一个控制器(但不这样),或者我可以在MyAttribute适用于所有行动除外措施3,但只是想,如果有一个更简单的方法?
I could potentially move this Action into another controller (but dont like that) or I could apply the MyAttribute to all actions except from Action3 but just thought if there is an easier way?
推荐答案
约翰内斯了正确的解决方案,这里是我如何codeD吧...希望它可以帮助其他人。
Johannes gave the correct solution and here is how I coded it... hope it helps other people.
[MyFilter("MyAction")]
public class HomeController : Controller
{
public ActionResult Action1...
public ActionResult Action2...
public ActionResult MyAction...
}
public class CompressFilter : ActionFilterAttribute
{
private IList _ExcludeActions = null;
public CompressFilter()
{
_ExcludeActions = new List();
}
public CompressFilter(string excludeActions)
{
_ExcludeActions = new List(excludeActions.Split(','));
}
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
HttpRequestBase request = filterContext.HttpContext.Request;
string currentActionName = (string)filterContext.RouteData.Values["action"];
if (_ExcludeActions.Contains(currentActionName))
return;
...
}
这篇关于MVC属性的控制器和动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文