50,126 Members
1 added today
271,899 Resources
17 added today

All Devdex   All Gurus  

A Simple solution for Rewriting Urls in Asp .Net
Author: Muthukumar V
Rating: Rate this Resource
Visits: 12193

793_RewriteLink.zip
Discuss in Newsgroups

Page:

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.

  1. Change the IIS Configuration Mappings of .html or any of the required extensions to be handled by aspnet_isapi.dll.
  2. 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>
  3. 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>
  4. Copy the RewriteLink.dll into the webapplication/bin folder.
  5. 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.


Visit my guru profile

<< Previous Page

Visitor Comments

Be the first to rate this article!

 

Rate this Article







	
	
	



ASP.NET Shopping Cart
Unlimited items/categories
Unlimited options/choices
Ecommerce toolkit for .NET!

ASP ArticlesThis category has been added to your weekly newsletter
ASP Web Sites
ADSI & WSH BooksThis category has been added to your weekly newsletter
FREE ComponentsThis category has been added to your weekly newsletter
ASP EventsThis category has been added to your weekly newsletter
ASP HeadlinesThis category has been added to your weekly newsletter

CSharp ArticlesThis category has been added to your weekly newsletter
C# Web SitesThis category has been added to your weekly newsletter

SQL ArticlesThis category has been added to your weekly newsletter
SQL Events
SQL HeadlinesThis category has been added to your weekly newsletter
SQL Jobs

Jobs in CaliforniaThis category has been added to your weekly newsletter

XML ArticlesThis category has been added to your weekly newsletter
XML BooksThis category has been added to your weekly newsletter
XML Web Sites
XML Tutorials

free asp host

"Alex Homer"This search has been added to your weekly newsletter

Edit My Favorites Edit Profile & Favorites

 




Developersdex Home | ASP | C# | SQL | VB | XML | Gurus
Add Your Link | Add Your Code | FAQ | Advertise | Link To Us | Contact Us |
Copyright © 2010 Developersdex™. All rights reserved.