March 1, 2021
Hot Topics:

How to Implement and Utilize URL Rewriting with ASP.NET

  • By David Consdorf
  • Send Email »
  • More Articles »

Now, in the web.config file you can add the following section and enter in your own URL rewrite rules. See the example below; it implements the following URL rewrite rules:

Input URL Actual URL
~/UserAccount/{UserID}.aspx ~/UserAccount.aspx?ID={UserID}
~/Movies/{MovieType}.aspx (Defined Set) ~/Movies.aspx?MovieType= {MovieType}

Table 2: URLRewriting.Net Example: URL Rewrite Rules

   defaultPage = "Default.aspx"
   xmlns="http://www.urlrewriting.net/schemas/config/2006/07" >
      <add name="UserAccount"
         ignoreCase="true" />
      <add name="Movies_Action"
         ignoreCase="true" />
      <add name="Movies_Drama"
         ignoreCase="true" />
      <add name="Movies_Comedy"
         ignoreCase="true" />

Listing 3: Configuring Rewrite Rules with URLRewriting.Net

As you can see, when using an HttpModule like URLRewriting.Net you can simply insert your web site's rewrite rules into the web.config file one by one without any code at all. This makes it very easy to manage the URL rewrite rules for your web site, large or small.

Also note that although the example here is pretty straightforward, URLRewriting.Net is a robust program with many more options than I go over here. Full documentation and a sample application can be found on the URLRewriting.Net web site.

Handling Postbacks

One potential problem that often arises when using URL rewriting methods with ASP.NET is that the clean input URL is not maintained on a page postback to the server. For example, you may enter a site using your nice clean URL such as "~/Movies/Action.aspx" but then when you click a button that performs a postback to the server, the URL will change back to the actual URL, "~/Movies.aspx?MovieType= Action".

The root of the problem lies with how the <form runat="server"> tag renders the action property. It uses the actual URL instead of the input one.

A simple way to remedy this problem is to write your own version of the form tag by extending the existing form tag. Then, you can set the action as the input URL instead of the rewritten one. See the example here:

public class RewriteForm : HtmlForm {

   protected override void
      RenderAttributes(System.Web.UI.HtmlTextWriter writer) {

      // Name
      writer.WriteAttribute("name", this.Name);

      // Method
      writer.WriteAttribute("method", this.Method);


      // Action
      String action = Context.Request.RawUrl;

      if (action != null) {
         writer.WriteAttribute("action", action);

      // ID
      if (base.ID != null) {
         writer.WriteAttribute("id", base.ClientID);
Listing 4: Custom form tag that can handle URL rewrite postbacks

Using the code above, you now can use the <MyTags:RewriteForm ID="form1" runat="server"> tag, instead of using the standard <form id="form1" runat="server"> tag,. This will keep the URL consistant even during postbacks.

Page 2 of 4

This article was originally published on October 8, 2007

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date