动作过滤器执行顺序
我创建了两个实现 AuthorizeAttribute 的类。
一个是全局使用的,我在 Global.asax.cs 上设置它:
filters.Add(new FirstAuthorizeAttribute() { Order = 0 });
另一个称为 SecondAuthorizeAttribute
,它仅在某些操作方法中使用,我将它用作我想要的方法中的属性。
[HttpGet]
[SecondAuthorize]
public ActionResult LogOut()
{
FormsAuthentication.SignOut();
Session.Clear();
Session.Abandon();
return Redirect(Url.Content("~/"));
}
问题是 SecondAuthorizeAttribute
总是在 FirstAuthorizeAttribute
之前执行,而我需要先执行这个。该命令没有帮助,我该怎么办?
I have created two classes that implement AuthorizeAttribute
.
One is used globally, and I set it on the Global.asax.cs:
filters.Add(new FirstAuthorizeAttribute() { Order = 0 });
The other is called SecondAuthorizeAttribute
and it is used only in some action methods, and I use it as attribute in the methods I want.
[HttpGet]
[SecondAuthorize]
public ActionResult LogOut()
{
FormsAuthentication.SignOut();
Session.Clear();
Session.Abandon();
return Redirect(Url.Content("~/"));
}
The problem is that SecondAuthorizeAttribute
always execute before FirstAuthorizeAttribute
, and I need this one to execute first. The order is not being helpful, how could I do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
@HectorCorrea 的答案中的链接目前已失效,以下是从当前 Google 缓存中检索和总结的内容(如果也出现这种情况):
过滤器按以下顺序执行:
每个过滤器内,您可以指定 Order 属性。 (所有过滤器均派生自抽象类 FilterAttribute,并且该类具有 Order 属性)。此属性将确保过滤器按特定顺序运行。
例如:
还有
FilterScope
,默认情况下,当顺序相同(或未指定)时,范围最低的过滤器首先运行:如果未指定顺序,则顺序值为 -1 (第一个,不是最后一个)。
控制器本身可以是过滤器,并将按
Int32.MinValue
的顺序运行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:
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:
There's also
FilterScope
and, by default, the filter with the lowest scope runs first when the order is the same (or not specified):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
这是一个漫长的过程,但是您是否尝试过使用 FirstAuthorizeAttribute 的 Global 和 First 值?
http://msdn.microsoft.com/en -us/library/system.web.mvc.filterscope(v=vs.98).aspx
http://blog.rajsoftware.com/post/2011/05/14/MVC3-过滤器排序.aspx
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
订单在 global.asax.cs 文件中不起作用。
如果要求是订单,则转到控制器或操作方法并给出。
例如:-
[第二个属性(顺序=1)]
[firstAttribute(order=2)]
HomeController:控制器
或
公共 ActionResult Index()
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()