Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Chameleon: A complete URL Rewriting framework

0.00/5 (No votes)
5 Apr 2004 1  
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.

Introduction

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.

Background

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.
    <httpModules> 
        <add type="Chameleon.Rewriter.CustomRewriteModule,Chameleon" 
        name="Chameleon.Rewriter" /> 
    </httpModules>
  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.)

    executable:C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\aspnet_isapi.dll
    extension : .*
    limitto:HEAD,GET,POST
    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.

License

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