ActionFilter 的顺序属性,从最低到最高还是反之亦然?
我定义了两个 ActionFilter:
[DefaultResources(Order = 2)]
[RenderTemplate(Order = 1)]
令我惊讶的是 DefaultResources 是在 RenderTemplate 之前执行的。但根据 MSDN 文档,它应该反之亦然:
[Filter1(Order = 2)]
[Filter2(Order = 3)]
[Filter3(Order = 1)]
public void Index()
{
View("Index");
}
在此示例中,操作过滤器将 按以下顺序执行: Filter3、Filter1、Filter2。
我正在使用 .NET 4。并通过 OnActionExecuted 方法进行比较。我错过了什么吗?
I defined two ActionFilters:
[DefaultResources(Order = 2)]
[RenderTemplate(Order = 1)]
And to my surprise DefaultResources is executed BEFORE RenderTemplate. But according to MSDN documentation it should work vice versa:
[Filter1(Order = 2)]
[Filter2(Order = 3)]
[Filter3(Order = 1)]
public void Index()
{
View("Index");
}
In this example, action filters would
execute in the following order:
Filter3, Filter1, and then Filter2.
I'm using .NET 4. And comparing by method OnActionExecuted. Am I missing something?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是我一直在寻找的答案。 OnActionExecuted 的顺序与 OnActionExecuting 的顺序相反...
This is the answer I was looking for. Order of OnActionExecuted is reversed order of OnActionExecuting...
这完全取决于每个过滤器实现的内容。
如果 DefaultResource 实现了 OnActionExecuting 或 OnActionExecuted,那么如果 RenderTemplate 没有实现,它将首先触发。
有关更多详细信息,请参阅:
http ://www.gregshackles.com/2010/09/custom-ordering-of-action-filters-in-asp-net-mvc/
和
http://msdn.microsoft.com/en-us/library/dd381609.aspx
"ASP.NET MVC 框架将调用操作过滤器的 OnActionExecuting 方法在调用任何使用操作过滤器属性标记的操作方法之前,框架将在操作方法完成后调用 OnActionExecuted 方法。
It all depends on what each filter implements.
If DefaultResource implements OnActionExecuting or OnActionExecuted then it will fire first if RenderTemplate does not.
For more details see:
http://www.gregshackles.com/2010/09/custom-ordering-of-action-filters-in-asp-net-mvc/
and
http://msdn.microsoft.com/en-us/library/dd381609.aspx
"The ASP.NET MVC framework will call the OnActionExecuting method of your action filter before it calls any action method that is marked with your action filter attribute. Similarly, the framework will call the OnActionExecuted method after the action method has finished. "
请参阅在 ASP.NET MVC 中进行过滤 完整解释了决定动作过滤器及其方法的执行顺序的因素。
关于您所说的过滤器正在使用的
OnResultExecuted
,请参阅以下内容:排序实际上相当复杂,因此请查看文章以了解更多详细信息。
See Filtering in ASP.NET MVC for a full explanation of what determines the order of execution of action filters and their methods.
Regarding
OnResultExecuted
, which you said your filters are using, see the following:The ordering is actually quite complex, so take a look at the article for more details.