Saturday, October 14, 2006

URL Mapping in ASP.NET 2.0

When URL mapping is useful?
URL mapping comes handy in number of situations:

  • You want to hide a big URL from the end user and present an easy to remember shorter URL
  • You want to pass query string parameters to a page but do not want them to be displayed to the user
  • You have changed web form names after the application is deployed
    How this works?

In ASP.NET 2.0 there is a configuration section called urlMappings that maps one URL to the other. The following markup shows how this section can be used:


<br /><system.web><br /><urlmappings enabled="true"><br /><add mappedurl="~/Site_Common/About.aspx" url="~/About.aspx"><br /><add mappedurl="~/Site_common/Legal.aspx" url="~/Legal.aspx"><br /></urlmappings><br /></system.web><br />

The enabled attribute of section enables or disables the URL mapping feature.
The sub section allows you to specify the actual mapping.
The url attribute of section specifies the URL as seen by the end user. It can be from browser address bar, hyper link or Response.Redirect statements.
The mappedUrl attribute species the URL that is actually served in place of the URL specified by url attribute.

You can specify as many URL mappings as you wish using multiple tags.