In this article we can see URL Mapping feature that is newly Introduced in ASP.Net 2.0. Search Engine Friendly URLs can be made for ASP.Net Pages using URL Mapping Feature in ASP.Net 2.0

Introduction to URL Mapping

In practical scenario there are many cases where URL content plays big role. Take a look.

  1. URL of any page grows so much and it looks very less user friendly. Thought that does not harm the application performance, it becomes less readable and not understandable.
  2. In some case where code folders are moved from one page to another we need to change the actual code inside the .net pages to map it to correct page.
  3. Revealing the actual folder structure in the URL shows the beneath directory structure. E.g based on URL http://www.xyz.com/Category/Products/Search.aspx clearly reveals the folder structure beneath it.

URL mapping is solution to above problems. It is a new feature introduced from ASP.Net 2.0. It enables page developers to map one set of URLs to another. If a request comes in for one of the URLs in the first set, it is automatically re-mapped on the server-side.

Example:

Application can be configured so that the URL ~/Architects.aspx URL is mapped to  ~/COMP/Specialist/DisplayByCategory.aspx?CategoryID=1&CategoryName=Architects. With such a mapping in place, when a user enters http://www.xyz.com/Architects.aspx into their browser’s Address bar, on the server-side the request will be handled as if they had entered http://www.xys.com/COMP/Specialist/DisplayByCategory.aspx?CategoryID=1&CategoryName=Architects. The user, however, will continue to see http://www.xys.com/Architects.aspx in their browser’s Address bar; they won’t know that the request was re-mapped.

URL mapping is used to provide "friendly" URLs, as Architects.aspx is a "friendly" URL and more readable than COMP/Specialists/DisplayByCategory.aspx?CategoryID=1&CategoryName=Architects.

URL mapping also is useful when re-structuring a site. If a new change is introduced in the site where all pages mapped to ~/COMP/Specialist directory pages will be accessed accessible through ~/COMP/Professionals, and if the users who have old links bookmarked or any external application accessing this path will get page not found error when they access the ~/COMP/Specialists/DisplayByCategory.aspx. This can be overcome by using URL mapping to map each page in the Specialists folder to its corresponding page in Professional.

Configure URL mappings

The Web.config file is used to configure URL mapping.

Following code added in web.config specifies the URL mappings for the application. The <urlMappings> element and added URL mapping specifies the complete mapping details

<configuration>
   ...
   <system.web>
     ...
     <urlMappings enabled="true">
       <add
    url="~/Architects.aspx"
    mappedUrl="~/COMP/Specialist/DisplayByCategory.aspx?CategoryID=1&CategoryName=Architects/>
        <add ...
     </urlMappings>
   </system.web>
</configuration> 

Note: Since the markup in Web.config is XML, it is important that reserved characters be escaped. The ampersand character (&), for example, should be escaped using &amp;, as the URLs in the mappedUrl attributes show.

Even when the page is visited through the friendly URL (Architects.aspx), the querystring parameters from the mapped URL are available. The code above illustrates this. Note that the user has entered into the browser Architects.aspx. When the request enters the server, it re-maps it to ~/ COMP/Specialist /DisplayByCategory.aspx? CategoryID=1&CategoryName=Architects, but the user keeps seeing Architects.aspx in the browser’s Address bar. But from the perspective of the server-side code, the querystring parameters CategoryID and CategoryName are present and are used in displaying the category’s name and its data.

Re-Mapping of URLs

When a request reaches the web server, it passes through a series of steps en route to being rendered. After proceeding through these steps, the ASP.NET runtime determines the requested URL, grabs that page’s corresponding class, and instructs the class to render itself. The page goes through its lifecycle, which includes initializing its control hierarchy and progressing through a number of stages – PreInit, Init, Load, and so forth – with the net result being the page is rendered into markup. This markup is then returned to the requesting browser and displayed.

One of the steps that occurs before the page is rendered examines the requested URL and checks to see if there are any matching URL mappings. If there are, it uses the HttpContext class’s RewritePath method. This method updates the current request’s URL to the one specified by the mapping rule in Web.config. With this information updated, when the page is ready to be rendered, the system sees that it’s supposed to render the mapped page, and uses that URL instead of the one initially requested by the user. The net result is that the mapped page is rendered, but the user (and their browser) thinks that they are visiting the page they requested.

Limitations of Built-in URL Mapping

There are two major shortcomings of ASP.NET 2.0’s built-in URL mapping feature.

  1. 1. It requires that the URL mappings be statically defined in Web.config. This is less than ideal for data-driven websites that use friendly URLs because it means someone has to manually enter the friendly URLs rather than having them read from the database dynamically.

    Example: If there is one more specialist category called Manager added in database then , you will have to add a new category mapping in web.config as well or remove in other case.
  2. 2. The other major shortcoming is the lack of regular expression support. In many case it is useful to be able to define a mapping pattern. This is especially helpful if using URL mapping to prevent broken URLs after a site restructuring. With the ability to use regular expressions in the rewriting rules, the following single mapping would map all requests to the Old folder to the same name in the New folder. (The assumption here being that the site was restructured and the Old folder was renamed to New.)

    <add url="~/Old/(.*)" mappedUrl="~/New/$1" />
    ASP.Net 2.0 doesn’t support regular expressions in its URL mapping implementation. We can implement this scenario using HTTPModule or URL Re-writing technique.
  3. Another, limitation is that a page visited through URL mapping has its <form>’s action element reference the actual, underlying page. That is, if a user visits the friendly URL Architects.aspx, which maps to COMP/Specialist /DisplayByCategory.aspx? CategoryID=1&CategoryName=Architects, when the page is posted back the user’s browser will request  DisplayByCategory.aspx?CategoryID=1&CategoryName= Architects and not Architects.aspx. Things will function normally, but the URL in the Address bar will change after the first post back, which is a strange behavior

Conclusion

In short, URL mapping provides a simple way to define a mapping from one set of URLs to another. This technique is commonly used to create "friendly" URLs or to handle potentially disruptive site restructurings. Unfortunately, the built-in URL mapping features are overly simplistic and don’t provide the features commonly needed in real-world scenarios. It is possible to provide such functionality, though, through a custom HTTP Module. IIS 7 is slated to include a greater degree of rewriting capabilities. Instead of going in complexities of HTTP module , URL Mapping provides most easiest solution.

Related Posts

  1. Routing in ASP.NET 4.0 vs Routing in ASP.Net 3.5
  2. Asp.net 4.0 Webform Features
  3. ASP.Net Performance Improvement Tips
  4. ASP.Net 4.0 | The ability to set meta tags
  5. Checklist/Guidelines for ASP.Net Developers

Tags: , , ,

One Comment to “Search Engine Friendly URL (URL Mapping) in ASP.Net 2.0”

  1. Deko Web says:

    Really nice article, thank you very much..

Leave a Reply

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>