Sunday, June 17, 2007

URL rewriting in ASP.Net 2.0! Purpose and how to?

URL rewriting is the process of intercepting an incoming Web request and automatically redirecting it to a different URL.

Creating data-driven ASP.NET websites often results in a single Web page that displays a subset of the database's data based on querystring parameters. For example, in designing an e-commerce site, one of your tasks would be to allow users to browse through the products for sale. To facilitate this, you might create a page called displayCategory.aspx that would display the products for a given category. The category's products to view would be specified by a querystring parameter. That is, if the user wanted to browse the Widgets for sale, and all Widgets had a had a CategoryID of 5, the user would visit: http://yousite.com/displayCategory.aspx?CategoryID=5.
There are two downsides to creating a website with such URLs. First, from the end user's perspective, the URL http://yousite.com/displayCategory.aspx?CategoryID=5 is a mess. Usability expert Jakob Neilsen recommends that URLs be chosen so that they:

  • Are short.
  • Are easy to type.
  • Visualize the site structure.
  • "Hackable," allowing the user to navigate through the site by hacking off parts of the URL.

I would add to that list that URLs should also be easy to remember. The URL http://yousite.com/displayCategory.aspx?CategoryID=5 meets none of Neilsen's criteria, nor is it easy to remember. Asking users to type in querystring values makes a URL hard to type and makes the URL "hackable" only by experienced Web developers who have an understanding of the purpose of querystring parameters and their name/value pair structure.

A better approach is to allow for a sensible, memorable URL, such as http://yoursite.com/products/Widgets. By just looking at the URL you can infer what will be displayed—information about Widgets. The URL is easy to remember and share, too. I can tell my colleague, "Check out yoursite.com/products/Widgets," and she'll likely be able to bring up the page without needing to ask me again what the URL was. (Try doing that with, say, an Amazon.com page!) The URL also appears, and should behave, "hackable." That is, if the user hacks of the end of the URL, and types in http://yoursite.com/products, they should see a listing of all products, or at least a listing of all categories of products they can view.

How to configure URL rewriting in asp.net 2.0?
Add the following code in web.config with proper mapping informations.

<?xml version="1.0" ?">
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
<system.web">
<urlmappings enabled="true"">
<add url="~/Autos.aspx" mappedurl="~/Default.aspx?category=autos">
<add url="~/Games.aspx" mappedurl="~/Default.aspx?category=games">
</urlmappings>
</system.web>
</configuration>

Now, add the mapping in the ISAPI filter of IIS.
Say, if you use the extention ".mspx", add the extension to the ISS pointing to C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll. That means, whatever request with extension .mspx should be handled by aspnet worker thread.

HTH

No comments: