ActionFilter 的顺序属性,从最低到最高还是反之亦然?

发布于 2024-11-10 16:06:47 字数 466 浏览 6 评论 0原文

我定义了两个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

决绝 2024-11-17 16:06:47

后进先出顺序

这是我一直在寻找的答案。 OnActionExecuted 的顺序与 OnActionExecuting 的顺序相反...

Last-in First-out order

This is the answer I was looking for. Order of OnActionExecuted is reversed order of OnActionExecuting...

月下伊人醉 2024-11-17 16:06:47

这完全取决于每个过滤器实现的内容。

如果 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. "

沩ん囻菔务 2024-11-17 16:06:47

请参阅在 ASP.NET MVC 中进行过滤 完整解释了决定动作过滤器及其方法的执行顺序的因素。

关于您所说的过滤器正在使用的 OnResultExecuted,请参阅以下内容:

OnActionExecuting(ActionExecutingContext)OnResultExecuting(ResultExecutingContext)OnAuthorization(AuthorizationContext) 过滤器按正向顺序运行。 OnActionExecuted(ActionExecutedContext)OnResultExecuting(ResultExecutingContext)OnException(ExceptionContext) 过滤器以相反的顺序运行。

排序实际上相当复杂,因此请查看文章以了解更多详细信息。

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 OnActionExecuting(ActionExecutingContext), OnResultExecuting(ResultExecutingContext), and OnAuthorization(AuthorizationContext) filters run in forward order. The OnActionExecuted(ActionExecutedContext), OnResultExecuting(ResultExecutingContext), and OnException(ExceptionContext) filters run in reverse order.

The ordering is actually quite complex, so take a look at the article for more details.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文