微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

asp.net-mvc-3 – MVC 3中的BeginRequest类过滤器?

我的应用程序中有一些代码,我需要在任何其他操作(即使在身份验证之前)执行每个请求.到目前为止,我一直在Global.asax中使用Application_BeginRequest事件,这一切都很好.但是这个代码需要打到数据库,而且由于某种原因,Global.asax这样做是不正确的.此外,我使用的Ninject.MVC3 nuget不会将依赖关系注入到我的HttpApplication ctor中.

所以我决定做的是将这个代码移动到自己的全局动作过滤器中.我现在遇到的问题是,无论什么Order或FilterScope我给这个过滤器,我不能让它先执行;我的授权过滤器总是打败它. MSDN似乎证实了这一点:

Filter Order

Filters run in the following order:

  1. Authorization filters
  2. Action filters
  3. Response filters
  4. Exception filters

For example,authorization filters run
first and exception filters run last.
Within each filter type,the Order
value specifies the run order. Within
each filter type and order,the Scope
enumeration value specifies the order
for filters.

我知道我可以使用一个HttpModule,但是并不感觉到MVCish,所以我试图在去之前耗尽所有可能性,这导致了我的问题:

是否有等同于全局动作过滤器的BeginRequest?

解决方法

您可以在基本控制器的 Initialize方法中执行此操作.

另一种可能性是注册一个global filter

public class MyGlobalFilter : ActionFilterattribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        // that's gonna be hit
    }
}

并在您的Global.asax的RegisterGlobalFilters事件中:

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
    filters.Add(new HandleErrorAttribute());
    filters.Add(new MyGlobalFilter());
}

原文地址:https://www.jb51.cc/aspnet/250669.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐