Click here to Skip to main content
13,248,927 members (40,509 online)
Click here to Skip to main content
Add your own
alternative version


60 bookmarked
Posted 31 Mar 2004

Chameleon: A complete URL Rewriting framework

, 5 Apr 2004
Rate this:
Please Sign up or sign in to vote.
This framework allows for separation of code and URLs for a website. The URLs can be specified in an XML configuration file and allow mapping to user controls.


This framework allows for separation of code and URLs for a website. The URLs can be specified in an XML configuration file and allow mapping to user controls.


Since code can be based on objects, design patterns and controls, they do not always match with URLs that are intuitive. The other reason for having custom URLs is that they can be technology independent. For example, the URL for a product page can be [sitename]/products/?category=1 instead of [sitename]/products.aspx?category. If we move from .aspx extension to .asx*(or anything else) we don't have to worry about our partners changing links to our pages.

Using the code

A word of warning:

The HttpModule doing a URLRewrite does not seem to let the web project to be opened in either versions of Visual Studio. So the workaround (I probably should spend more time on this...or NOT) is to comment out the Web.Config reference to the rewriter module once you are done and ready to check in your code. When you open the web project, you can uncomment the reference to the HTTP Module in the web.config.

Using the framework:

  1. Copy Chameleon.dll to bin directory
  2. Add a reference to it
  3. Add HTTP Module information to the web.config under the system.web section. Note: Remove this when closing/checking-in the project since this does not allow for the project to be opened from Visual Studio .NET.
        <add type="Chameleon.Rewriter.CustomRewriteModule,Chameleon" 
        name="Chameleon.Rewriter" /> 
  4. Go to the Internet Information Services Admin utility. Add a new entry to IIS admin| Virtual Directory | Configuration |Mappings tab for the virtual directory your project is running in. Here are the values for each field: (Also, here is reference to this technique in an article by Richard Kirby called URL Rewriting with ASP.NET.)

    extension : .*
    script engine checked
    check the file exists unchecked
  5. Change the form tags in default.aspx to use our custom form object (which inherits from HTMLForm).
    <%@ Register TagPrefix="Chameleon" 
      Namespace="Chameleon.Rewriter" assembly="Chameleon"%>

    Replace the form tag with <CHAMELEON:CUSTOMHTMLFORM id="FormDefault" runat= "server"></CHAMELEON:CUSTOMHTMLFORM>

How it works

There are 3 important sections of the code that lets it function the way it does.

  1. HttpModule that does the rewrite.
  2. The second section is trying to fix the problem of posting back to the correct URL (the one seen in the browser i.e., /products/) not the rewritten one (default.aspx?etc etc). The article by Richard Kirby called URL Rewriting with ASP.NET solved the rewrite problem but not the postback problem. That is what I have addressed in this article.

    Since ASP.NET assigns variable to the action tag in the default form object, we have to create our own form that inherits from HtmlForm and overwrites the action tag.

  3. The third section lets us load a user control (.ascx) specified in the configuration XML file. This section could have been a part of the core Chameleon DLL but was deliberately kept outside to give the flexibility for people to be able to modify or customize it to suit their needs.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Web Developer
Switzerland Switzerland
Rahul Agarwal is a software consultant and is currently working with ASP.NET.

You may also be interested in...

Comments and Discussions

GeneralFor Simplest Way of writing URL Rewriting Pin
DotNetGuts24-Jul-08 11:34
memberDotNetGuts24-Jul-08 11:34 
QuestionRewriting using MasterPage Pin
jctools27-Jan-08 6:04
memberjctools27-Jan-08 6:04 
GeneralITs URGENT Pin
shhhhhhhhhh23-Apr-07 0:49
membershhhhhhhhhh23-Apr-07 0:49 
NewsUrl Rewriting for virtual directories Pin
Claudiu_i28-Nov-06 6:02
memberClaudiu_i28-Nov-06 6:02 
GeneralRe: Url Rewriting for virtual directories Pin
fahdansari18-Feb-07 10:58
memberfahdansari18-Feb-07 10:58 
Questionpostback events? Pin
lolailo22225-Jun-05 2:32
memberlolailo22225-Jun-05 2:32 
Generali know more!! Pin
wons198020-Dec-04 20:58
memberwons198020-Dec-04 20:58 
QuestionWhat's up with Forms Authentication? Pin
Trip-l21-Nov-04 6:25
memberTrip-l21-Nov-04 6:25 
GeneralForms authentication Pin
Kotulan5-Jun-04 22:28
memberKotulan5-Jun-04 22:28 
GeneralImages Pin
Uncle Monkey22-Apr-04 1:01
memberUncle Monkey22-Apr-04 1:01 
GeneralRe: Images Pin
Rahul.Net22-Apr-04 6:28
memberRahul.Net22-Apr-04 6:28 
GeneralRe: Images Pin
Uncle Monkey22-Apr-04 6:40
memberUncle Monkey22-Apr-04 6:40 
GeneralRe: Images Pin
Rahul.Net22-Apr-04 9:10
memberRahul.Net22-Apr-04 9:10 
GeneralRead this before opening the demo project Pin
Rahul.Net2-Apr-04 5:53
memberRahul.Net2-Apr-04 5:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171114.1 | Last Updated 6 Apr 2004
Article Copyright 2004 by Rahul.Net
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid