Asp.net Permalinks Using URL Rewriting
A Permalink is a permanent link which points to a particular blog entry or forum entry. For example the permalink for this blog article is http://csharp-codesamples.com/2009/03/aspnet-permalinks-using-url-rewriting.
This does not mean that a physical page is created for each blog entry. Instead a single page shows the data for multiple blogs articles dynamically based on some query string.
But all pages in asp.net end with a .aspx extension and if we pass a query string to the page it will look something like Page.aspx?query=test. These links are not very search engine friendly.
This article describes a simple method to create permalinks and improve your asp.net websites search engine optimization using URL rewriting without the use of custom handlers.
Building The URL
Let us consider that http://csharp-codesamples.com/ is our blog site and Default.aspx is the main page that handles the requests made to the blog.
In most cases we use virtual directories for hosting websites. So if we had a virtual directory called blog then our main url would look like http://csharp-codesamples.com/blog.
Any request made to this url should be handled by the Default.aspx page. To achieve this we add a Global.asax file to our website (in visual studio add new items to website and select Global Application Class).
Add a Application_BeginRequest method to Global.asax. This method is invoked automatically whenever a request comes to the server.
Absolute url for a request http://csharp-codesamples.com/blog will be /blog. we have to rewrite it to /blog/Default.aspx as shown below.
To make this code a bit more generic we derive the application path.
If a virtual path does not exist (web site is hosted in root directory) then we need to take the appPath as empty string. the default value of appPath in case of root directory hosting is ‘/’.
Handling Direct Requests
The above piece of code works fine when a request is made to http://csharp-codesamples.com/blog. But what if due to some carelessness in code a request is made directly to http://csharp-codesamples.com/blog/Default.aspx. We will have to handle these request and redirect them to http://csharp-codesamples.com/blog.
You could have a business class written to rewrite and redirect these url based on a url repository. For example http://csharp-codesamples.com/2009/03/aspnet-permalinks-using-url-rewriting needs to be redirected to http://csharp-codesamples.com/Articles.aspx?id=6.
Handling Postbacks
This method will be ineffective when a postback is made by a button press or any other such action. On postback the response object will rewrite the original url which was used to handle the request, that is http://csharp-codesamples.com/blog/Default.aspx.
The reason for this problem is that asp.net sets the form action as post by default. To overcome this problem we need to register the following script on each page
If there is a master page then this script can be inserted at the end justy before </body> tag or by calling the following line on each page.
Images And CSS
For images and CSS the relative path is taken as src. Which means the src for an image which is in the root directory that is /blog/image1.png will taken as /blog/article/image1.png. since the image is not at that location it will not be found.
To avoid this problem the simplest solution is use of Asp.net Themes and skins instead of directly using css links or images. also for images use the asp.net image control instead of the standard html <img> tag and use the ~ sign for root directory i.e. ~/image1.png.
Alternatives
This article describes a very basic method for url rewriting and it aims at making you understand how urls are handled. Even though the methods works perfectly, I would advise you to have a look at ScottGu’s Blog for a more in depth article on url rewriting.
Note: Make sure you configure IIS to handle urls without extensions. Refer Configuring WebDAV Server in IIS 7, 6, 5
Download Sample
UrlRewritingSample.zip
good ….
Very helpful. And I won’t forget to read Scott Gu as well. Thanks.