Redirect URL Using HttpModule Asp.net

2020-03-22 08:24发布

I have created an HttpModule so that Whenever I type "localhost/blabla.html" in the browser, it will redirect me to www.google.com (this is just an example, it's really to redirect requests coming from mobile phones)

My Questions are :

1) How do I tell IIS(7.0) to redirect each request to the "HttpModule" so that it is independent of the website. I can change the web.config but that's it.

2) Do I need to add the .dll to the GAC? If so, How can I do that?

3) The HttpModule code uses 'log4net' . do I need to add 'log4net' to the GAC as well?

Thanks

P.S. the site is using .net 2.0.

1条回答
再贱就再见
2楼-- · 2020-03-22 08:59

You can use request object in BeginRequest event

public class MyHttpModule : IHttpModule
{
    public void Init(HttpApplication context)
    {
          context.BeginRequest += new EventHandler(this.context_BeginRequest);
    }

    private void context_BeginRequest(object sender, EventArgs e)
    {
          HttpApplication application = (HttpApplication)sender;
          HttpContext context = application.Context;

          //check here context.Request for using request object 
          if(context.Request.FilePath.Contains("blahblah.html"))
          {
               context.Response.Redirect("http://www.google.com");
          }
    }

}
查看更多
登录 后发表回答