August 1, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

How to Implement and Utilize URL Rewriting with ASP.NET

  • October 8, 2007
  • 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

<urlrewritingnet
   rewriteOnlyVirtualUrls="true"
   contextItemsPrefix="QueryString"
   defaultPage = "Default.aspx"
   defaultProvider="RegEx"
   xmlns="http://www.urlrewriting.net/schemas/config/2006/07" >
   <rewrites>
      <add name="UserAccount"
           virtualUrl="^~/UserAccountV2/(.+).aspx"
         rewriteUrlParameter="ExcludeFromClientQueryString"
         destinationUrl="~/UserAccountV2.aspx?id=$1"
         ignoreCase="true" />
      <add name="Movies_Action"
           virtualUrl="^~/Movies/Action.aspx"
         rewriteUrlParameter="ExcludeFromClientQueryString"
         destinationUrl="~/Movies.aspx?MovieType=Action"
         ignoreCase="true" />
      <add name="Movies_Drama"
           virtualUrl="^~/Movies/Drama.aspx"
         rewriteUrlParameter="ExcludeFromClientQueryString"
         destinationUrl="~/Movies.aspx?MovieType=Drama"
         ignoreCase="true" />
      <add name="Movies_Comedy"
           virtualUrl="^~/Movies/Comedy.aspx"
         rewriteUrlParameter="ExcludeFromClientQueryString"
         destinationUrl="~/Movies.aspx?MovieType=Comedy"
         ignoreCase="true" />
   </rewrites>
</urlrewritingnet>

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);
      base.Attributes.Remove("name");

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

      this.Attributes.Render(writer);

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

      if (action != null) {
         writer.WriteAttribute("action", action);
      }
      base.Attributes.Remove("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



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel