Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect URL Using HttpModule Asp.net

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.

like image 366
orit cohen Avatar asked Jul 24 '12 14:07

orit cohen


People also ask

How do I redirect a URL in ASP NET Core?

URL Rewriting Middleware is provided by the Microsoft.AspNetCore.Rewrite package, which is implicitly included in ASP.NET Core apps. Establish URL rewrite and redirect rules by creating an instance of the RewriteOptions class with extension methods for each of your rewrite rules.

How to redirect a client to another page in ASP NET?

ASP.NET performs the redirection by returning a 302 HTTP status code. An alternative way to transfer control to another page is the Transfer method. The Transfer method is typically more efficient because it does not cause a round trip to the client. For more information, see How to: Redirect Users to Another Page. Redirects a client to a new URL.

What is redirect request?

Redirects a request to a new URL and specifies the new URL. Redirects a client to a new URL. Specifies the new URL and whether execution of the current page should terminate. Redirects a request to a new URL and specifies the new URL.

What is redirecttoaction method in ASP NET Core?

The RedirectToAction method is used to redirect a request in ASP.NET Core from one URL to another. This can be used to redirect based on some condition. The method is part of the Controllerbase class so it’s directly available for use in the controller class.


1 Answers

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

}
like image 160
Govind Malviya Avatar answered Sep 23 '22 00:09

Govind Malviya