Search code examples
.netasp.net-mvcattributesaction-filter

In ASP.NET MVC, how to define in which sequence my custom attributes are checked/applied?


I'm currently investigating the possibility to use custom attributes derived from ActionFilterAttribute. I want to accomplish a couple of things with a couple of attributes. The thing is I want to make sure one of the attributes comes into play first, and not in any random sequence.

Example:

public class Feature1Attrubute : ActionFilterAttribute
{
    /* ... */
}

public class Feature2Attrubute : ActionFilterAttribute
{
    /* ... */
}

public class MyController : Controller
{
    [Feature1, Feature2]
    public ActionResult MyAction ()
    {
        /* ... */
    }
}

Is it so that attributes are applied in the sequence they mentioned in the method decoration?

If not, is there a way to define a particular sequence for a group of [custom] attributes?


Solution

  • The base class ActionFilterAttribute has a property called Order. That's what you're looking for.

    public class MyController : Controller
    {
        [Feature1(Order = 1), Feature2(Order = 2)]
        public ActionResult MyAction ()
        {
            /* ... */
        }
    }