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());
}

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....