加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

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

发布时间:2020-12-15 23:39:21 所属栏目:asp.Net 来源:网络整理
导读:我的应用程序中有一些代码,我需要在任何其他操作(即使在身份验证之前)执行每个请求.到目前为止,我一直在Global.asax中使用Application_BeginRequest事件,这一切都很好.但是这个代码需要打到数据库,而且由于某种原因,Global.asax这样做是不正确的.此外,我使用
我的应用程序中有一些代码,我需要在任何其他操作(即使在身份验证之前)执行每个请求.到目前为止,我一直在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());
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读