基础控制器中较早执行的 ActionFilterAttribute - MVC2

发布于 2024-10-19 18:29:56 字数 698 浏览 2 评论 0原文

我有以下设置。当在 GuestDetailsController 中执行操作时,如何在 [RequireCheckoutAvailability] 之前运行 [LoadThemeInfo],而无需在子控制器的属性上指定顺序? 过滤器都使用 OnActionExecuting。

[LoadThemeInfo(Order=1)]
public class MgControllerBase : Controller
{

}

[RequireCheckoutAvailability(Order=2)]
public class GuestDetailsController : MgControllerBase
{

}

来自 msdn Order 属性采用一个整数值,该值必须为 0(默认值)或更大,但有一个例外。省略 Order 属性会使过滤器的顺序值为 -1,这表示未指定的顺序。 Order 属性设置为 -1 的作用域中的任何操作过滤器都将以不确定的顺序执行,但会在具有指定顺序的过滤器之前执行。

我希望未指定的操作过滤器在具有指定顺序的过滤器之后执行案件。还有另一种方法可以实现我想要实现的目标吗?也许将加载主题信息代码移出属性并移至其他位置?

I have the following setup. When an action is executed in GuestDetailsController, how can I have[LoadThemeInfo] run prior to [RequireCheckoutAvailability] without having to specify orders on the child controller's attributes? The filters all use OnActionExecuting.

[LoadThemeInfo(Order=1)]
public class MgControllerBase : Controller
{

}

[RequireCheckoutAvailability(Order=2)]
public class GuestDetailsController : MgControllerBase
{

}

From msdn: The Order property takes an integer value that must be 0 (the default) or greater, with one exception. Omitting the Order property gives the filter an order value of -1, which indicates an unspecified order. Any action filter in a scope whose Order property is set to -1 will be executed in an undetermined order, but before the filters that have a specified order.

I would prefer the unspecifieds execute after those with a specified order in this case. Is there another way to do what I am trying to achieve? Maybe move load theme info code out of the attribute and somewhere else?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

牵强ㄟ 2024-10-26 18:29:56

在属性的构造函数中设置顺序。为了将执行顺序保持在一处并在添加新属性时易于维护,请创建一个 enum 例如:

 public enum CustomActionFilterOrder{
     LoadThemeInfo = 1,
     RequireCheckoutAvailability = 2,
 }

并且 Attribute 构造函数将如下所示

public RequireCheckoutAvailability(){
    this.Order = (int)CustomActionFilterOrder.RequireCheckoutAvailability;
}

Set the order in the constructor of the attribute. In order to keep the execution orders in one place and keep it easy to maintain as new attributes get added, create an enum such as:

 public enum CustomActionFilterOrder{
     LoadThemeInfo = 1,
     RequireCheckoutAvailability = 2,
 }

and the Attribute constructor will look like

public RequireCheckoutAvailability(){
    this.Order = (int)CustomActionFilterOrder.RequireCheckoutAvailability;
}
So尛奶瓶 2024-10-26 18:29:56

从基本控制器中取出 ActionFilterAttribute 并从重写的 OnActionExecuting 调用它,使其比子控制器上的属性更早执行。

public class MgControllerBase : Controller
{
    protected override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        var filter = new LoadThemeInfoFilter(DependencyLookup.Resolve<IBrandContext>());
        filter.OnActionExecuting(filterContext);

        base.OnActionExecuting(filterContext);
    }
}

[RequireCheckoutAvailability]
public class GuestDetailsController : MgControllerBase
{

}

Taking the ActionFilterAttribute off the base controller and calling it from an overridden OnActionExecuting makes it execute earlier than attributes on child controllers.

public class MgControllerBase : Controller
{
    protected override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        var filter = new LoadThemeInfoFilter(DependencyLookup.Resolve<IBrandContext>());
        filter.OnActionExecuting(filterContext);

        base.OnActionExecuting(filterContext);
    }
}

[RequireCheckoutAvailability]
public class GuestDetailsController : MgControllerBase
{

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