ASP.NET MVC 4拦截所有传入的请求

时间:2012-07-30 17:51:35

标签: asp.net-mvc asp.net-mvc-4

有没有办法让我能够将所有传入的请求捕获到我的ASP.NET MVC 4应用程序并运行一些代码,然后再继续请求到指定的控制器/操作?

我需要使用现有服务运行一些自定义身份验证代码,为了正确执行此操作,我需要能够拦截来自所有客户端的所有传入请求,以便使用其他服务仔细检查某些内容。

4 个答案:

答案 0 :(得分:77)

最正确的方法是创建一个继承ActionFilterAttribute并覆盖OnActionExecuting方法的类。然后可以在GlobalFilters

中的Global.asax.cs中注册

当然,这只会拦截实际拥有路线的请求。

答案 1 :(得分:37)

您可以使用HttpModule来完成此任务。以下是我用于计算所有请求的处理时间的示例:

using System;
using System.Diagnostics;
using System.Web;

namespace Sample.HttpModules
{
    public class PerformanceMonitorModule : IHttpModule
    {

        public void Init(HttpApplication httpApp)
        {
            httpApp.BeginRequest += OnBeginRequest;
            httpApp.EndRequest += OnEndRequest;
            httpApp.PreSendRequestHeaders += OnHeaderSent;
        }

        public void OnHeaderSent(object sender, EventArgs e)
        {
            var httpApp = (HttpApplication)sender;
            httpApp.Context.Items["HeadersSent"] = true;
        }

        // Record the time of the begin request event.
        public void OnBeginRequest(Object sender, EventArgs e)
        {
            var httpApp = (HttpApplication)sender;
            if (httpApp.Request.Path.StartsWith("/media/")) return;
            var timer = new Stopwatch();
            httpApp.Context.Items["Timer"] = timer;
            httpApp.Context.Items["HeadersSent"] = false;
            timer.Start();
        }

        public void OnEndRequest(Object sender, EventArgs e)
        {
            var httpApp = (HttpApplication)sender;
            if (httpApp.Request.Path.StartsWith("/media/")) return;
            var timer = (Stopwatch)httpApp.Context.Items["Timer"];

            if (timer != null)
            {
                timer.Stop();
                if (!(bool)httpApp.Context.Items["HeadersSent"])
                {
                    httpApp.Context.Response.AppendHeader("ProcessTime",
                                                          ((double)timer.ElapsedTicks / Stopwatch.Frequency) * 1000 +
                                                          " ms.");
                }
            }

            httpApp.Context.Items.Remove("Timer");
            httpApp.Context.Items.Remove("HeadersSent");

        }

        public void Dispose() { /* Not needed */ }
    }

}

这就是你在Web.Config中注册模块的方式:

<system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
        <add name="PerformanceMonitorModule" type="Sample.HttpModules.PerformanceMonitorModule" />
    </modules>
<//system.webServer>

答案 2 :(得分:22)

我认为你搜索的是:

Application_BeginRequest()

http://www.dotnetcurry.com/showarticle.aspx?ID=126

你把它放在Global.asax.cs

    protected void Application_BeginRequest(object sender, EventArgs e)
    {
        HttpContext.Current.Request.....;
    }

我将此用于调试目的,但我不确定它对您的案例有多好的解决方案。

答案 3 :(得分:2)

我不确定MVC4,但我认为它与MVC5非常相似。如果您已创建新的Web项目 - &gt;查看Global.asax,您应该在方法FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);中看到以下行Application_Start()

RegisterGlobalFilters是文件FilterConfig.cs中位于App_Start文件夹中的方法。

正如@ YngveB-Nilsen所说ActionFilterAttribute是我认为的方式。添加一个派生自System.Web.Mvc.ActionFilterAttribute的新类。这很重要,因为例如System.Web.Http.Filters.ActionFilterAttribute将失败并出现以下异常。

  

给定的过滤器实例必须实现以下一项或多项   过滤器接口:System.Web.Mvc.IAuthorizationFilter,   System.Web.Mvc.IActionFilter,System.Web.Mvc.IResultFilter,   System.Web.Mvc.IExceptionFilter,   System.Web.Mvc.Filters.IAuthenticationFilter。

将请求写入调试窗口的示例:

public class DebugActionFilter : System.Web.Mvc.ActionFilterAttribute
{
  public override void OnActionExecuting(ActionExecutingContext actionContext)
  {
    Debug.WriteLine(actionContext.RequestContext.HttpContext.Request);
  }
}

FilterConfig - &gt; RegisterGlobalFilters - &gt;添加以下行:filters.Add(new DebugActionFilter());

您现在可以捕获所有传入的请求并进行修改。