Saturday, September 19, 2009

ASP.NET - URL Mapping in Web.config

The URL mapping feature uses configuration information stored in web.config to remap incoming requests to a different URL. The remapping occurs prior to any other processing for the inbound request. Although the sample below demonstrates remapping a page request, any arbitrary file type can have its request remapped to a different URL.

Imagine some body kept the URL of your web site in their bookmarks and you want to change the pages and structure of your site so that it affects the URLs, then you need to redirect the old ones to new correct destination, it works if you consider URL mapping.

The URL mapping feature uses to redirect incoming requests to a different URL. Prior to any other processing for the incoming request the remapping happens.

URL Mapping stores in web.config file, inside <system.web> tag, it's possible to add elements in <urlMappings>. In <add> tag there is url attribute which points to exact value of incoming url and mappedUrl attribute keeps value of rewritten url which is the exact redirected destination.


<configuration>
   <system.web>
      <urlMappings>
         <add url="~/LCDs.aspx" mappedUrl="~/prds.aspx?prdCat=lcd" />
         <add url="~/DVDs.aspx" mappedUrl="~/prds.aspx?prdCat=dvd" />
         <add url="~/MP3s.aspx" mappedUrl="~/prds.aspx?prdCat=mp3" />
      </urlMappings>
   </system.web>
</configuration>


With URL mapping, the redirection works the same as the Server.Transfer() method, and browser will still show the original incoming request URL, not the mapped URL.

The Request.RawUrl property returns the original request URL.
The Request.Path property reflects the result of mapped URL.
Request.QueryString["yourQueryStringElement"] also return the result from the mapped URL.

If we have a site map provider in our web page, it first try to use the original request URL when looking for the node in the site map which is provided by Request.RawUrl, if it doesn't find the matched record, it uses the Request.Path property.

Incomming Request --->
   Mapping available in Web.config?
      --- YES ---> Use mappedUrl path --->
                                                      [Render requested path]
      --- NO --------------------------------->
Share/Bookmark

No comments: