Click here to Skip to main content
15,885,141 members
Articles / Web Development / ASP.NET
Article

Chameleon: A complete URL Rewriting framework

Rate me:
Please Sign up or sign in to vote.
3.39/5 (8 votes)
5 Apr 20043 min read 85.4K   1.6K   59   14
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).
    ASP.NET
    <%@ 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


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

Comments and Discussions

 
GeneralFor Simplest Way of writing URL Rewriting Pin
DotNetGuts24-Jul-08 10:34
DotNetGuts24-Jul-08 10:34 
QuestionRewriting using MasterPage Pin
jctools27-Jan-08 5:04
professionaljctools27-Jan-08 5:04 
GeneralITs URGENT Pin
NidhiKanu22-Apr-07 23:49
professionalNidhiKanu22-Apr-07 23:49 
NewsUrl Rewriting for virtual directories Pin
Claudiu_i28-Nov-06 5:02
Claudiu_i28-Nov-06 5:02 
GeneralRe: Url Rewriting for virtual directories Pin
fahdansari18-Feb-07 9:58
fahdansari18-Feb-07 9:58 
Questionpostback events? Pin
lolailo22225-Jun-05 1:32
lolailo22225-Jun-05 1:32 
Generali know more!! Pin
wons198020-Dec-04 19:58
wons198020-Dec-04 19:58 
QuestionWhat's up with Forms Authentication? Pin
Trip-l21-Nov-04 5:25
Trip-l21-Nov-04 5:25 
GeneralForms authentication Pin
rkotulan5-Jun-04 21:28
rkotulan5-Jun-04 21:28 
GeneralImages Pin
Uncle Monkey22-Apr-04 0:01
Uncle Monkey22-Apr-04 0:01 
GeneralRe: Images Pin
Rahul.Net22-Apr-04 5:28
Rahul.Net22-Apr-04 5:28 
GeneralRe: Images Pin
Uncle Monkey22-Apr-04 5:40
Uncle Monkey22-Apr-04 5:40 
GeneralRe: Images Pin
Rahul.Net22-Apr-04 8:10
Rahul.Net22-Apr-04 8:10 
GeneralRead this before opening the demo project Pin
Rahul.Net2-Apr-04 4:53
Rahul.Net2-Apr-04 4: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.