mvc拦截请求IHttpModule

 

代码:

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

 

转载于:https://www.cnblogs.com/Tpf386/p/10254411.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要捕获m3u8请求,您可以使用ASP.NET中的HttpModuleHttpHandler。HttpModule是一种可重用的类,可拦截和处理请求和响应。HttpHandler是一种可执行文件,可以处理特定类型的请求。 以下是使用HttpModule捕获m3u8请求的示例代码: ```csharp public class M3u8Module : IHttpModule { public void Init(HttpApplication app) { app.BeginRequest += new EventHandler(OnBeginRequest); } private void OnBeginRequest(object sender, EventArgs e) { HttpApplication app = (HttpApplication)sender; HttpContext context = app.Context; if (context.Request.Url.AbsolutePath.EndsWith(".m3u8")) { //处理m3u8请求 } } public void Dispose() { } } ``` 在此示例中,您可以在OnBeginRequest方法中检查请求的URL是否以“.m3u8”结尾。如果是,则可以处理请求。 要使用HttpHandler捕获m3u8请求,请创建一个继承自IHttpHandler的类,并实现ProcessRequest方法。然后,在Web.config文件中将处理程序映射到m3u8扩展名。 以下是使用HttpHandler捕获m3u8请求的示例代码: ```csharp public class M3u8Handler : IHttpHandler { public void ProcessRequest(HttpContext context) { if (context.Request.Url.AbsolutePath.EndsWith(".m3u8")) { //处理m3u8请求 } } public bool IsReusable { get { return true; } } } ``` 在Web.config文件中添加以下内容: ```xml <system.webServer> <handlers> <add name="M3u8Handler" path="*.m3u8" verb="*" type="YourNamespace.M3u8Handler" /> </handlers> </system.webServer> ``` 使用HttpModuleHttpHandler捕获m3u8请求都可以实现相同的目的。您可以根据您的需要选择其中一种方法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值