Rusty S. Swayne

tidbits and tangents

Rusty S. Swayne

My name is Russell S. Swayne although I have gone by Rusty all of my life. I own a small web design/development studio named Mindfly (http://www.mindfly.com), have a wonderful wife and very precocious and yet extremely charming daughter.

contact me Feed
 

Friendly URLs, ASP.NET 2.0 and Shared Hosting

One of the main goals of rewriting urls is to create urls that a website user will find easy to remember and intuitive to type. As web designers and developers, we want to create truly friendly urls such as http://www.mindfly.com/contact. However, in a Windows shared hosting environment running an ASP.NET application rewriting the URL http://www.mindfly.com/default.aspx?node=5 to http://www.mindfly.com/contact is a little more difficult than one would think.

There are quite a few examples of URL rewriting techniques for ASP.NET online. When I first started researching this topic I found that the DotNetNuke Open Source Project and the ThunderMain.URLRewriter objects were great tools. With the release ASP.NET 2.0, URL rewriting became even easier with the urlMappings section in the web.config file. Currently, I use a URL rewriter object for dynamically generated pages and the urlMappings web.config section for polishing at the end of a web development project due to the fact that each URL mapping must be listed individually.

All of these approaches are straight forward but have their limitations. In my opinion, one the biggest limitations is that you must have the ability to configure IIS to pass all requests through the ASP.NET ISAPI extension if you want to handle documents other than those with an .aspx extension. This poses a problem when you plan on hosting your application in a shared environment because most hosts will not give you direct access to the IIS mmc.

To me, it was not immediately obvious to how to handle rewrite of the more user friendly URL http://www.mindfly.com/contact because it does not have the .aspx extension. Ironically the solution I settled on was really simple. In Mindfly's root directory I created a /contact directory and added a default.aspx (without any code) so that the virtual path to this file is ~/contact/default.aspx. Without url rewriting, http://www.mindfly.com/contact would return an empty page, but default.aspx has an .aspx extension, and thus the ASP.NET ISAPI extension is now able to handle the request which makes the handling url rewrite possible with pretty much any of the aforementioned techniques. Obviously this method should be used sparingly and is not practical for more than a few pages as it requires you to create a directory for each page.

Step by step for an ASP.NET 2.0 application

  1. Create the /contact directory
  2. Add a default.aspx page to the contact directory
  3. Add the urlMapping to the web.config
    (Note urlMappings must be a child node of node in the web.config file)

<urlMappings enabled="true">
     <add url="~/contact.aspx" mappedUrl="~/default.aspx?node=5" />
     <add url="~/contact/default.aspx" mappedUrl="~/default.aspx?node=5" />
</urlMappings>

Providing easy to remember urls for your website visitors is relatively simple and definitely worth a little extra time.

Comments

Rusty

Rusty said...

I just read an article by Scott Guthrie that gives an example of performing an extension-less url rewrite if your host has IIS 7.0.
weblogs.asp.net/.../...rewriting-with-asp-net.aspx

-July 5. 2007 01:09

Add comment


(Will show your Gravatar icon)  





Live preview

said...

-July 6. 2008 08:56

 
 

Powered by BlogEngine.NET 1.2.0.0. Original Design by Heather Alvis.
Sign in