In what order are filters executed in asp.net mvc

前端 未结 2 1467
有刺的猬
有刺的猬 2020-12-02 18:20

In MVC we can decorate action methods with different filters like

[HttpPost]
[Authorize]
public ActionResult mymethod(){}

HttpPost

相关标签:
2条回答
  • 2020-12-02 18:53

    To save you some time, this is how you set the order:

    [MyCustomContextFilter(Order=1)]
    

    The index is 0 based, so you can do 0, 1, 2, etc...

    It should be noted that just because a filter is on the base class doesn't tell MVC to apply it first :(.

    0 讨论(0)
  • 2020-12-02 19:01

    Filters run in the following order:

    1. Authorization filters
    2. Action filters
    3. Response filters
    4. Exception filters

    For example, authorization filters run first and exception filters run last. Within each filter type, the Order value specifies the run order. Within each filter type and order, the Scope enumeration value specifies the order for filters. This enumeration defines the following filter scope values (in the order in which they run):

    1. First
    2. Global
    3. Controller
    4. Action
    5. Last

    Extracted from MSDN

    0 讨论(0)
提交回复
热议问题