代码:
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Diagnostics;namespace TestMVCRoute{ ???public class myMVCHttpHandler :IHttpModule ???{ ???????public void Dispose() ???????{ ???????????/* Not needed */ ???????} ???????public void Init(HttpApplication context) ???????{ ???????????context.BeginRequest += OnBeginRequest; ???????????context.EndRequest += OnEndRequest; ???????????context.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"); ???????} ???}}
webconfig注册:
?<httpModules> ?????<add name="myMVCHttpHandler" type="TestMVCRoute.myMVCHttpHandler"/> ?</httpModules>
</system.web>
或者节点:
<modules runAllManagedModulesForAllRequests="true"> ?????<add name="myMVCHttpHandler" type="TestMVCRoute.myMVCHttpHandler" /> ???</modules></system.webServer>
ii6和ii7及以上注册节点结构不同需要注意,所以才贴出了两点。
改文章复制粘贴自:https://stackoverflow.com/questions/11726848/asp-net-mvc-4-intercept-all-incoming-requests
文章标题:ASP.NET MVC 4 intercept all incoming requests
不过前辈们建议,你最好分清你需要拦截的是什么请求,你的目的是什么,如果是在知情Action控制前拦截,那么你最好使用过滤器,而不是注册一个自定义的IHttpModule。
文章:msdn.microsoft.com/en-us/library/gg416513(VS.98).aspx
或者也可以直接在Global.asax.cs文件中使用:
protected void Application_BeginRequest(object sender, EventArgs e) ???{ ???????HttpContext.Current.Request.....; ???}
默认mvc的Global.asax.cs中没有Application_BeginRequest方法,但是自己复制粘贴过去会起作用,原理是什么还不清楚。
最建议的拦截mvc请求的方式还是用过滤器,
public class DebugActionFilter : System.Web.Mvc.ActionFilterAttribute{ ?public override void OnActionExecuting(ActionExecutingContext actionContext) ?{ ????Debug.WriteLine(actionContext.RequestContext.HttpContext.Request); ?}}
mvc拦截请求IHttpModule
原文地址:https://www.cnblogs.com/Tpf386/p/10254411.html