MVC attributes on controllers and actions

Is there a way to add an attribute at the controller level, but not to a specific action. For example, let's say if I had 10 actions in my controller, and only 1 of these actions does not require the special attribute that I created.

[MyAttribute]
public class MyController: Controller
{
    public ActionResult Action1 () {}
    public ActionResult Action2 () {}

    [Remove_MyAttribute]
    public ActionResult Action3 () {}
}

I can potentially transfer this action to another controller (but don't like it), or I can apply MyAttribute to all actions except Action3, but just thought there is an easier way?

+5
source share
5 answers

, ... , .

[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;

        ...
    }
+3

/ , . ( [Remove_MyAttribute]).

+3

, ( ) , , , , , - .

AttributeUsage, ( !), (/) . "" , : , ( ) ( ) ( ).

:

[AttributeUsage(AttributeTargets.Class|AttributeTargets.Method, Inherited=true, AllowMultiple=false)]
public class MyCustomFilterAttribute : ActionFilterAttribute
{

    private MyCustomFilterMode _Mode = MyCustomFilterMode.Respect;        // this is the default, so don't always have to specify

    public MyCustomFilterAttribute()
    {
    }
    public MyCustomFilterAttribute(MyCustomFilterMode mode)
    {
        _Mode = mode;
    }

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        if (_Mode == MyCustomFilterMode.Ignore)
        {
            return;
        }

        // Otherwise, respect the attribute and work your magic here!
        //
        //
        //
    }

}

public enum MyCustomFilterMode
{
    Ignore = 0,
    Respect = 1
}

( , , ! , : /, .)

:

[MyCustomFilter]
public class MyBaseController : Controller
{
    // I am the application base controller with the filter,
    // so any derived controllers will ALSO get the filter (unless they override/Ignore)
}

public class HomeController : MyBaseController
{
    // Since I derive from MyBaseController,
    // all of my action methods will also get the filter,
    // unless they specify otherwise!

    public ActionResult FilteredAction1...
    public ActionResult FilteredAction2...

    [MyCustomFilter(Ignore)]
    public ActionResult MyIgnoredAction...    // I am ignoring the filter!

}

[MyCustomFilter(Ignore)]
public class SomeSpecialCaseController : MyBaseController
{
    // Even though I also derive from MyBaseController, I can choose
    // to "opt out" and indicate for everything to be ignored

    public ActionResult IgnoredAction1...
    public ActionResult IgnoredAction2...

    // Whoops! I guess I do need the filter on just one little method here:
    [MyCustomFilter]
    public ActionResult FilteredAction1...

}

, , - , .

+3

, :

 [MyAttribute(Exclude="Action3")]

( : VB.NET, , , ), :

<Models.MyAttribute(Exclude:="Action3")> _
Public Class MyController
Inherits System.Web.Mvc.Controller

End Class
+2

, , , , .

:

[ComVisible] which is equivalent with [ComVisible(true)]

or 

[ComVisible(false)]

inf , :

[MyAttribute] // defaults to true

and

[MyAttribute(false)] for applying the attribute on excluded members
+2

All Articles