Creating a RewriteLink Handler:
This is a httpModule written implementing the IHttpModule interface in ASP .Net. This IHttpModule is implemented internally by the Page class and HttpApplication in ASP .Net. This interface contains many functions which can be implemented based on our needs. Whenever the web application runs, the ASP .Net run time checks the web.config to see if there are any modules of type IHttpModule to be loaded. The entries in web.config looks like this:
<system.web> <httpModules> <add type="RewriteLink.LinkWriter,RewriteLink" name="RewriteLink" /> </httpModules> <system.web> |
When ASP .Net executes the application, it will check to see if there are any modules under the System.web in the config file. If there are any, it will load the module and this is the place(module) where we handle the url rewriting mechanism.
Create a class library project with a name as required. The sample project in this article uses RewriteLink as the project name. The class which implements the IHttpModule is named as LinkWriter. The following code snippet explains how we do this.
public class LinkWriter : System.Web.IHttpModule {
public void Init(System.Web.HttpApplication appl) { appl.BeginRequest += new System.EventHandler(UrlWrite_BeginRequest); }
public void Dispose() { }
public void UrlWrite_BeginRequest(object sender,System.EventArgs e) {
System.Web.HttpApplication appl = (System.Web.HttpApplication)sender;
string src = "request_url.html"; string dest = "destination_url.aspx";
//Check if the request and our collection has the data if(appl.Request.Path.ToLower() == src.ToLower()) { //Rewrite the path appl.Context.RewritePath(dest); } }
}
|
The above code snippet does not read the redirection details from anywhere. But the sample project contains a new section under configSections entry inside web.config. This contains a section named as RewriterSettings, which is made to contain the required entries for request source and destination path.
When the httpModule is instantiated, ASP .Net also supplies the HttpApplication instance as the parameter to the derived class (in our case it is LinkWriter). The HttpApplication class contains number of events like AuthenticateRequest, BeginRequest etc., This module can be extended to support custom authentication mechanisms, url rewriting etc., The event of our interest is BeginRequest, which is to be subscribed by our object derived from IHttpModule. This will be used to intercept the request and rewrite it to a different path.
The HttpApplication member function RewritePath takes care of rewriting the path to a different destination. This modifies the request path to the new destination specified by the RewritePath function. Once this is done, the new page is automatically returned to IIS and goes back to the browser.
The class library dll required along with the sample application is attached here with this article.
Modifying the ASP .Net application:
The RewriteLink.dll in the sample application of this article can be used for any existing or new asp .net application. Just ensure the following things are in place.
- Change the IIS Configuration Mappings of .html or any of the required extensions to be handled by aspnet_isapi.dll.
- Create a new configSections as follows.
<configSections> <section name="RewriterSettings" type="System.Configuration.NameValueSectionHandler,System, Version=1.0.3300.0,Culture=neutral, PublicKeyToken=b77a5c561934e089,Custom=null"/> </configSections> <RewriterSettings> <add key="/Webform1.html" value="/Webform1.aspx" /> </RewriterSettings> |
- Add a httpModule section under system.web as explained in the web.config snippet above in the previous section.
<system.web> <httpModules> <add type="RewriteLink.LinkWriter,RewriteLink" name="RewriteLink" /> </httpModules> <system.web> |
- Copy the RewriteLink.dll into the webapplication/bin folder.
- Now if we build our web application and type http://localhost/rewriteapp/webform1.html, then it automatically gets redirected to the webform1.aspx page.
The beauty of the above request is the client browser still shows the url as http://localhost/rewriteapp/webform1.html and it does not even know that something has happened behind the browser. All the jobs are taken care at server level and the page is returned.
The attached sample class library contains only a simple mechanism of url forwarding. This can be extended with RegEx support for different type of urls and files.
Conclusion:
The above mechanism can be extended for any type of extensions. For example if a website wants to convert from a .asp to .aspx, then it can be done seamlessly. For html sites, this has proved to be a powerful mechanism of serving pages. For example a big html based static website can easily be converted to .aspx based site which will simplify the site management. Though this extra effort puts a little extra overhead on the server, it is worth the extra time.