So here we go URL re-writing in ASP.Net. What URL re-writing means is that you intercept an incoming web request in your web application and then redirect the web request to a different web resource in your web application. Now this is not done simply using Response.Redirect or Server.Redirect.
There are many reasons why you would choose to do URL re-writing and the major
ones could be -
- You want your urls to be search engine friendly.
- Your website has undergone restructuring or you expect the folders to be moved
arround later. - You want your urls to be user friendly (as in easier to remember).
querystring information and currently requested resource.
Steps to implement URL rewriting -
- Create a website in ASP.Net
- Add a class (URLRewriter) and make it implement IHTTPModule interface.
- Add an entry in the web config's HTTPModule section so that your module gets picked up
- Add an eventhandler for BeginRequest event by using the HTTPContext object in
the Init method. - In the BeginRequest event handler type cast the sender to HTTPApplication and
extract the current context - Analyse the RawURL from the Request object of the current context to ascertain
if rewrite is required - If yes then use the context objects RewritePath method to redirect to a
different resource. - Insert a base tag with base address on your master page
You will need to do one last thing before you can say that your url rewriting is working fine. At this stage if you run your application and check the source of the html that is rendered on the browser you will notice that there is an action tag in the form element and it is not pointing to the rewritten path or the new path that you used to redirect from the HTTPModule. This poses a problem as it will hinder in your normal page post backs. So you need to modify the form tag rendered by removing the action tag. You can do this using different approaches.
- You can create a control adaptor which you can attach to the html form tag using
a browser file. - You can create a class that inherits from HTMLForm class and use it instead of
the form tag in your page. - You can create a class that inherits from Page class, override its Render
method and use it to inherit all your form's code behind classes from.
That is it. Thats all you need to know about URL rewriting.
HTTP Module Class
public class URLRewriter:IHttpModule
{
public void Dispose()
{
}
public void Init(HttpApplication context)
{
context.BeginRequest +=
new EventHandler(context_BeginRequest);
}
void context_BeginRequest(object sender, EventArgs e)
{
HttpApplication httpapp = sender as HttpApplication;
if (httpapp != null)
{
HttpContext currcontext = httpapp.Context;
string rawURL = currcontext.Request.RawUrl;
//your url might be of the form -
http://mysite.com/modid/1/techid/5/productid/10/productdetail
//analyze rawURL to see if you need to redirect the request
//analyze rawURL to extract any parameter information
string strQS = //generate and store normal query string here
//rewrite the new path here
currcontext.RewritePath("~/" + targetpage + strQS);
}
}
}
HTTPModule entry in the web.config file
<configuration>
<system.web>
<httpModules>
<add name="URLRewriter"
type="<namspace_namehere>.URLRewriter" />
</httpModules>
</system.web>
</configuration>
Base tag in the master page.
<base href="http://www.yourwebsitename.com/" />
</head >
Page base class
public class PageBase:System.Web.UI.Page
{
protected override void
Render(System.Web.UI.HtmlTextWriter writer)
{
StringWriter swriter = new StringWriter();
HtmlTextWriter hwriter = new HtmlTextWriter(swriter);
base.Render(hwriter);
hwriter.Flush();
string html = swriter.ToString();
hwriter.Dispose();
int spos = 0;
spos = html.IndexOf("action=\"");
if (spos > 0)
{
int epos = 0;
epos = html.IndexOf("\"", spos + 8) + 1;
html = html.Remove(spos, epos - spos);
}
writer.Write(html);
}
}
Inherit Form Class from your new Page Base class
{ }
Comments