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:
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:
- Copy Chameleon.dll to bin directory
- Add a reference to it
- 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.
- 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
- Change the
form tags in default.aspx to use our custom form object (which inherits from
<%@ Register TagPrefix="Chameleon"
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.
HttpModule that does the rewrite.
- 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
- 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.