Friday, January 23, 2009

URL Remapping in .Net 2.0

In this article I will walk through a concept for URL Remapping in .Net 2.0

The need for url remapping can arise due to several reasons like
Generating SEO friendly urls
Hiding the actual page or other data from web users.
Control over each and every entry and exits for web requests.
Etc.
.Net 2.0 gives flexibility to define and remap urls in web.config file as well as generating your own Custom URL Remapper HTTP Module.
I will explain the first approach i.e remap urls in web.config in this post and will try to wrap up the second one in my next article with a pretty decent code sample for building Custom URL Remapper HTTP Module.
Remapping URL's in web.config file.
To Map urls .Net provides a section named  urlMappings in web.config file where one can define the collection for their mapped urls.

[sourcecode language='html']


 
  
 



[/sourcecode]

Here whenever the request for Home.aspx will come the .Net engine will search for  urlMappings section in web.config file and server the page that is mapped with it. In the above mention example it will server Default.aspx page.
Limitations: To have more control over the urls, regular expression cannot be used. So it is better to build our own  URL Remapper HTTP Module.
Live World Scenario:
In case of shopping cart with thousands of product categories and catalogs it is very important to make this page SEO Friendly. Though SEO includes several criteria depending on different robots basic idea of URL remains the same. So by using URL Remapping we can generate SEO friendly URLS that robots can easily search.
Like say for example we are having three products categories viz
CellPhones
Computers
DigitalCameras
We can map all the three categories to a single page that servers the purpose of displaying these products based on the product name
So we map all the three to Product.aspx page like shown in the below section.

[sourcecode language='html']


 
  
 
 
 



[/sourcecode]

Now whenever the request for  CellPhones.aspx,  Computers.aspx or DigitalCameras.aspx comes .Net engine will compile Product.aspx page and will server to the requesting client. In Product.aspx page we can extract the name and make calls to database to get all the products related to the given category.

 

To Download code sample Click Here