Articles → .NET → URL rewriting using HTTPModules

URL rewriting using HTTPModules

















public class UrlRewriter : IHttpModule{}




public void Dispose()
{ }
public void Init(HttpApplication app)
{ }




public void Init(HttpApplication app)
{
  app.BeginRequest += new EventHandler(Rewrite_BeginRequest);
}
void Rewrite_BeginRequest(object sender, EventArgs e)
{}








protected void Button1_Click(object sender, EventArgs e)
{
  Response.Redirect("Admin");
}
protected void Button2_Click(object sender, EventArgs e)
{
  Response.Redirect("Contact");
}




void Rewrite_BeginRequest(object sender, EventArgs e)
{        
  HttpApplication appObject = (HttpApplication)sender;
  HttpContext contextObject = appObject.Context;
  string path = contextObject.Request.Url.AbsoluteUri;   
  if (!path.EndsWith(".aspx"))
  {
    path = path.Split('/')[path.Split('/').Length - 1];
    contextObject.RewritePath("Default2.aspx?module=" + path);
}        
  }






<httpModules>
  <add name="rewriter" type="CustomClass" />
</httpModules>




protected void Page_Load(object sender, EventArgs e)
{
  if (!IsPostBack)
  {
    string module = Request["module"].ToString();
    Response.Write(String.Format("You have entered {0} module", module));
  }
}







Click to Enlarge




Posted By  -  Karan Gupta
 
Posted On  -  Sunday, November 21, 2010

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250