Wednesday, January 5, 2011

URL Mapping in ASP.Net 2.0

URL mapping is often used to provide "friendly" URLs, which are URLs that are more readable and sensical
- Beverages.aspx is a "friendly" URL as it is more readable than ProductsByCategory.aspx?CategoryID=1&CategoryName=Beverages. URL mapping also is useful when restructuring a site.
Imagine that all product information was accessible through http://YourSite.com/Products/...,
but the higher-ups want the products rooted available through http://YourSite.com/Catalog/...
instead. Clearly, any users who have the old links bookmarked or linked from a website will get 404s if they visit
once the folder name change has been made. This can be mitigated by using URL mapping to map each page in
the Products folder to its corresponding page in Catalog.

ASP.Net 2.0 provides a simple way to map a long URL to a short URL with the help of   <urlMappings> tag in
Web.Config. With this feature URL rewritting becomes simple but with some big limitations.

For example,

  <urlMappings>
      <add url="~/Csharp.aspx"       
           mappedUrl="~/Articles/Csharp/Default.aspx"/>
      <add url="~/ASPNet.aspx"        
           mappedUrl="~/Articles/ASPNet/Default.aspx"/>
      <add url="~/Framework.aspx"        
           mappedUrl="~/Articles/Framework/Default.aspx"/>
    </urlMappings>


The above config settings routes the user to the URL given in mappedUrl property and thus makes the
URL very simple and easily rememberable.
The drawback of this feature is when our websites grows drastically then maintaining this section in
config file will be tedious.  To overcome this we can build a URL rewriting module using Regular expression.

No comments :

Post a Comment