分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 运营维护

mvc拦截请求IHttpModule

发布时间:2023-09-06 02:29责任编辑:郭大石关键词:暂无标签

代码:

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

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved