URL Mapping in ASP
URL Mapping in ASP.NET 2.0
Introduction
Few months back I wrote an article titled
URL Rewriting in ASP.NET that
illustrates how URL rewriting can be accomplished via code. ASP.NET 2.0 doesn't
change the situation too much but has added some handy configuration options for
URL mapping. In URL mapping a URL1 is mapped with URL2 so that when user
requests URL1 actually it is the URL2 that is served.
When URL mapping is useful?
URL mapping comes handy in number of situations. Below are some examples:
- 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:
<urlMappings enabled="true">
<add url="~/articles.aspx" mappedUrl="~/articles/default.aspx"/>
<add url="~/forum.aspx" mappedUrl="~/forum/default.aspx"/>
<add url="~/blog.aspx" mappedUrl="~/blog/default.aspx"/>
</urlMappings>
As you can see the enabled attribute of <urlMappings> section enables or
disables the URL mapping feature. The <add> sub section allows you to specify
the actual mapping. The url attribute of <add> 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. This in above
example if user requests articles.aspx from the browser address bar then
default.aspx page from articles folder is served. You can specify as many URL
mappings as you wish using multiple <add> tags.
In order to see URL mapping in action you need to first create a new web
site. Design the Default.aspx as shown below:

As you can see there are three HyperLink controls. The NavigateUrl property
of the HyperLink controls is set to articles.aspx, forum.aspx and blog.aspx
respectively. The Text property of each hyper link is set as shown in above
figure.
Then add three sub folder to your web site - Articles, Blog and Forum. Add a
new web form named Default.aspx in each of the folder. Drag and drop a Label
control on each default.aspx and set its Text property to "This is Articles
Folder", "This is Blog folder" and "This is Forum folder" respectively.
Add web.config file (if it is not present already) and key in the <urlMapping>
section shown above inside the <system.web> section.
Now you are ready to run the application. Set Default.aspx from the root
folder as the start up page of the web site and run. Click on "Click here to
read Articles" hyper link. You will notice that though the browser address bar
and hyper link shows the URL as articles.aspx the content rendered is from
~/articles/default.aspx. The following figure shows this fact.

Limitations of URL mapping feature
There are several limitations of URL mapping feature provided in ASP.NET 2.0.
Some of them include:
- The mapping provided by URL mapping feature is static
- You can change URL and mapped URL dynamically
- Though you can have query string parameters in the URLs they can not be
changed at run time
- You can not use this feature effectively to create URLs of the form
forum.somedomain.com
That's it for now. Stay tuned.