Action filter execution order

和自甴很熟 提交于 2019-12-03 04:55:06

This is a long shot, but have you tried using the Global and First values for your FirstAuthorizeAttribute ?

http://msdn.microsoft.com/en-us/library/system.web.mvc.filterscope(v=vs.98).aspx

http://blog.rajsoftware.com/post/2011/05/14/MVC3-Filter-Ordering.aspx

The link in @HectorCorrea's answer is dead at the moment, here's the content retrieved and summarised from the current Google cache (in case that also goes) :

Filters execute in this order:

  • Authorization filters
  • Action filters
  • Response/Result filters
  • Exception filters

Within each filter, you may specify the Order property. (All filters are derived from the abstract class FilterAttribute, and this class has an Order property). This property will ensure the filter runs in a specific Order.

eg:

[AuthorizationFilterA(Order=2)]
[AuthorizationFilterB(Order=1)]
public ActionResult Index()
{          
    return View();
}

There's also FilterScope and, by default, the filter with the lowest scope runs first when the order is the same (or not specified):

namespace System.Web.Mvc {
    public enum FilterScope {
        First = 0,
        Global = 10,
        Controller = 20,
        Action = 30,
        Last = 100,
    }
}

If no order is specified, the order value is -1 (first, not last).

Controllers themselves can be filters and will run with order Int32.MinValue

Order is not working in global.asax.cs file. If requirement is order then goto Controller or Action Method and give. ex:-

[secondAttribute(order =1)] [firstAttribute(order=2)]

HomeController:Controller

or

public ActionResult Index()

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!