A Complete URL Rewriting Solution for ASP.NET 2.0

Comments 0

Share to social media

This article describes a complete solution for URL rewriting in ASP.NET 2.0. The solution uses regular expressions to specify rewriting rules and resolves possible difficulties with postback from pages accessed via virtual URLs.

Why use URL rewriting?

The two main reasons to incorporate URL rewriting capabilities into your ASP.NET applications are usability and maintainability.

Usability

It is well-known that users of web applications prefer short, neat URLs to monstrous addresses packed with difficult to comprehend query string parameters. From time to time, being able to remember and type in a concise URL is less time-consuming than adding the page to a browser’s favorites to access later. Again, when access to a browser’s favorites is unavailable, it can be more convenient to type in the URL of a page on the browser address bar, without having to remember a few keywords and type them into a search engine in order to find the page.

Compare the following two addresses and decide which one you like more:

  1. http://www.somebloghost.com/Blogs/Posts.aspx?Year=2006&Month=12&Day=10
  2. http://www. somebloghost.com/Blogs/2006/12/10/

The first URL contains query string parameters to encode the date for which some blog engines should show available postings. The second URL contains this information in the address, giving the user a clear idea of what he or she is going to see. The second address also allows the user to hack the URL to see all postings available in December, simply by removing the text encoding the day ’10’: http://www.somehost.com/Blogs/2006/12/.

Maintainability

In large web applications, it is common for developers to move pages from one directory to another. Let us suppose that support information was initially available at http://www.somebloghost.com/Info/Copyright.aspx and http://www.somebloghost.com/Support/Contacts.aspx, but at a later date the developers moved the Copyright.aspx and Contacts.aspx pages to a new folder called Help. Users who have bookmarked the old URLs need to be redirected to the new location. This issue can be resolved by adding simple dummy pages containing calls to Response.Redirect(new location). However, what if there are hundreds of moved pages all over the application directory? The web project will soon contain too many useless pages that have the sole purpose of redirecting users to a new location.

Enter URL rewriting, which allows a developer to move pages between virtual directories just by editing a configuration file. In this way, the developer can separate the physical structure of the website from the logical structure available to users via URLs.

Native URL mapping in ASP.NET 2.0

ASP.NET 2.0 provides an out-of-the-box solution for mapping static URLs within a web application. It is possible to map old URLs to new ones in web.config without writing any lines of code. To use URL mapping, just create a new urlMappings section within the system.web section of your web.config file and add the required mappings (the path ~/ points to the root directory of the web application):

Thus, if a user types http://www.somebloghost.com/Support/Contacts.aspx, he can then see the page located at http://www.somebloghost.com/Help/Contacts.aspx, without even knowing the page had been moved.

This solution is fine if you have only two pages that have been moved to other locations, but it is completely unsuitable where there are dozens of re-located pages, or where a really neat URL needs to be created.

Another possible disadvantage of the native URL mapping technique is that if the page Contacts.aspx contains elements initiating postback to the server (which is most probable), then the user will be surprised that the URL http://www.somebloghost.com/Support/Contacts.aspx changes to http://www.somebloghost.com/Help/Contacts.aspx. This happens because the ASP.NET engine fills the action attribute of the form HTML tag with the actual path to a page. So the form renders like this:

Thus, URL mapping available in ASP.NET 2.0 is almost always useless. It would be much better to be able to specify a set of similar URLs in one mapping rule. The best solution is to use Regular Expressions (for overview see Wikipedia and for implementation in .NET see MSDN), but an ASP.NET 2.0 mapping does not support regular expressions. We therefore need to develop a different solution to built-in URL mapping.

The URL rewriting module

The best way to implement a URL rewriting solution is to create reusable and easily configurable modules, so the obvious decision is to create an HTTP Module (for details on HTTP Modules see MSDN Magazine) and implement it as an individual assembly. To make this assembly as easy to use as possible, we need to implement the ability to configure the rewrite engine and specify rules in a web.config file.

During the development process we need to be able to turn the rewriting module on or off (for example if you have a bug that is difficult to catch, and which may have been caused by incorrect rewriting rules). There should, therefore, be an option in the rewriting module configuration section in web.config to turn the module on or off. So, a sample configuration section within web.config can go like this:

This means that all requests that run like: http://localhost/Web/2006/12/10/ should be internally redirected to the page Posts.aspx with query string parameters.

Please note that web.config is a well-formed XML file, and it is prohibited to use the symbol & in attribute value strings. In this case, you should use & instead in the destination attribute of the rule element.

To use the rewriteModule section in the web.config file, you need to register a section name and a section handler for this section. To do this, add a configSections section to web.config:

This means you may use the following section below the configSections section:

Another thing we have to bear in mind during the development of the rewriting module is that it should be possible to use ‘virtual’ URLs with query string parameters, as shown in the following: http://www.somebloghost.com/2006/12/10/?Sort=Desc&SortBy=Date. Thus we have to develop a solution that can detect parameters passed via query string and also via virtual URL in our web application.

357-G_Magdanurov-feb07.gif

So, let’s start by building a new Class Library. We need to add a reference to the System.Web assembly, as we want this library to be used within an ASP.NET application and we also want to implement some web-specific functions at the same time. If we want our module to be able to read web.config, we need to add a reference to the System.Configuration assembly.

Handling the configuration section

To be able to read the configuration settings specified in web.config, we have to create a class that implements the IConfigurationSectionHandler interface (see MSDN for details). This can be seen below:

The Class RewriteModuleSectionHandler will be initialized by calling the Create method with the rewriteModule section of web.config passed as XmlNode. The SelectSingleNode method of the XmlNode class is used to return values for module settings.

Using parameters from rewritten URL

When handling virtual URLS such as http://www. somebloghost.com/Blogs/gaidar/?Sort=Asc (that is, a virtual URL with query string parameters), it is important that you clearly distinguish parameters that were passed via a query string from parameters that were passed as virtual directories. Using the rewriting rules specified below:

You can use the following URL:

http://www. somebloghost.com/gaidar/?Folder=Blogs

…and the result will be the same as if you used this URL:

http://www. somebloghost.com/Blogs/gaidar/

To resolve this issue, we have to create some kind of wrapper for ‘virtual path parameters’. This could be a collection with a static method to access the current parameters set:

You can see from the above that it is possible to access ‘virtual path parameters’ via the RewriteContext.Current collection and be sure that those parameters were specified in the URL as virtual directories or pages names, and not as query string parameters.

Rewriting URLs

Now let’s try some rewriting. First, we need to read rewriting rules from the web.config file. Secondly, we need to check the actual URL against the rules and, if necessary, do some rewriting so that the appropriate page is executed.

We create an HttpModule:

When adding the RewriteModule_BeginRequest method that will process the rules against the given URL, we need to check if the given URL has query string parameters and call HttpContext.Current.RewritePath to give control over to the appropriate ASP.NET page.

We must then register this method:

But this is just half of the road we need to go down, because the rewriting module should handle a web form’s postbacks and populate a collection of ‘virtual path parameters’. In the given code you will not find a part that does this task. Let’s put ‘virtual path parameters’ aside for a moment. The main thing here is to handle postbacks correctly.

If we run the code above and look through the HTML source of the ASP.NET page for an action attribute of the form tag, we find that even a virtual URL action attribute contains a path to an actual ASP.NET page. For example, if we are using the page ~/Posts.aspx to handle requests like:

http://www. somebloghost.com/Blogs/2006/12/10/Default.aspx,

…we find the action=”/Posts.aspx”. This means that the user will be using not the virtual URL on postback, but the actual one: http://www. somebloghost.com/Blog.aspx. This is not what we want to use here! So, a few more lines of code are required to achieve the desired result.

First, we must register and implement one more method in our HttpModule:

This method checks if the user requested a normal ASP.NET page and adds a handler for the PreInit event of the page lifecycle. This is where RewriteContext will be populated with actual parameters and a second URL rewriting will be performed. The second rewriting is necessary to make ASP.NET believe it wants to use a virtual path in the action attribute of an HTML form.

Finally, we see three classes in our RewriteModule assembly:

357-G_Magdanurov%282%29-feb07%20%282%29.

Registering RewriteModule in web.config

To use RewriteModule in a web application, you should add a reference to the rewrite module assembly and register HttpModule in the web application web.config file. To register HttpModule, open the web.config file and add the following code into the system.web section:

Using RewriteModule

There are a few things you should bear in mind when using RewriteModule:

  • It is impossible to use special characters in a well-formed XML document which is web.config in its nature. You should therefore use HTML-encoded symbols instead. For example, use & instead of &.
  • To use relative paths in your ASPX pages, you should call the ResolveUrl method inside HTML tags: <img src=”<%=ResolveUrl(“~/Images/Test.jpg”)%>” />. Note, that ~/ points to the root directory of a web application.
  • Bear in mind the greediness of regular expressions and put rewriting rules to web.config in order of their greediness, for instance

  • If you would like to use RewriteModule with pages other than .aspx, you should configure IIS to map requests to pages with the desired extensions to ASP.NET runtime as described in the next section.

IIS Configuration: using RewriteModule with extensions other than .aspx

To use a rewriting module with extensions other than .aspx (for example, .html or .xml), you must configure IIS so that these file extensions are mapped to the ASP.NET engine (ASP.NET ISAPI extension). Note that to do so, you have to be logged in as an Administrator.

Open the IIS Administration console and select a virtual directory website for which you want to configure mappings.

Windows XP (IIS 5)
Virtual Directory “RW”

357-G_Magdanurov%283%29-feb07.gif

Windows 2003 Server (IIS 6)
Default Web Site

357-G_Magdanurov%284%29-feb07.gif

Then click the Configuration… button on the Virtual Directory tab (or the Home Directory tab if you are configuring mappings for the website).

Windows XP (IIS 5)

357-G_Magdanurov%285%29-feb07.gif

Windows 2003 Server (IIS 6)

357-G_Magdanurov%286%29-feb07.gif

Next, click on the Add button and type in an extension. You also need to specify a path to an ASP.NET ISAPI Extension. Don’t forget to uncheck the option Check that file exists.

357-G_Magdanurov%287%29-feb07.gif

If you would like to map all extensions to ASP.NET, then for IIS 5 on Windows XP you have only to map .* extension to the ASP.NET ISAPI extension. But for IIS 6 on Windows 2003 you have to do it in a slightly different way: click on the Insert… button instead of the Add… button, and specify a path to the ASP.NET ISAPI extension.

357-G_Magdanurov%288%29-feb07.gif

Conclusions

Now we have built a simple but very powerful rewriting module for ASP.NET that supports regular expressions-based URLs and page postbacks. This solution is easily implemented and gives users the ability to use short, neat URLs free of bulky Query String parameters. To start using the module, you simply have to add a reference to the RewriteModule assembly in your web application and add a few lines of code to the web.config file, whereupon you have all the power of regular expressions at your disposal to override URLs. The rewrite module is easily maintainable, because to change any ‘virtual’ URL you only need to edit the web.config file. If you need to test your application without the module, you can turn it off in web.config without modifying any code.

To gain a deeper insight into the rewriting module, take a look through the source code and example attached to this article. I believe you’ll find using the rewriting module a far more pleasant experience, than using the native URL mapping in ASP.NET 2.0.

Load comments

About the author

Gaidar Magdanurov

See Profile

Gaidar Magdanurov is the President at Acronis. He oversees partner enablement, education, and marketing organizations. He started at Acronis in 2013 as a Business Manager to the CEO, progressing to Vice President of Online Business and General Manager for Consumer Business in 2016, then to Chief Marketing Officer in 2017, Chief Success Officer in 2020, and President in 2023. He is currently based in Sofia, Bulgaria. Prior to Acronis, he was an Investment Director at Runa Capital, focusing on seed-stage investments, advising entrepreneurs, supporting business development of portfolio companies, and maintaining relationships with startup incubators and accelerators. Before Runa Capital, Gaidar held various positions at Microsoft, including Technology Evangelist and Managing Director of the Microsoft Seed Fund. His earlier career involved roles as a software engineer and IT administrator for various companies. Gaidar holds a master’s degree in chemistry. You can follow Gaidar Magdanurov on LinkedIn: https://www.linkedin.com/in/gaidar/

Gaidar Magdanurov's contributions