2006-01-14

Simple custom URL rewriting in ASP.NET 1.1

This post can also be found here

This post describes how you can create a simple url rewriting engine in ASP.NET in order to create custom urls for your webpage..
I’ve used the great tutorial on 15 seconds, Rewrite.NET - a URL Rewriting engine for .NET written by Robert Chartier and modified some minor parts.
Instead of fetching the urls from inside the web.config file, I’ve used a simple xml document to store my customs paths.
I have also found a method for preventing the postback issue and will describe on how to change the file extensions in your urls.

We will start off by creating a xml file and calling it MyUrls.xml. I chosed to move the content from the web.config file because I whish to be able to easily change the urls from a web interface that's not causing the project to reboot which will happen when you re-save the web.config file.

<?xml version="1.0" encoding="utf-8" ?>
<urls>
<url custom="/project/Start.aspx" real="/Project/index.aspx?id=123" />
<url custom="/Project/Custom.aspx" real="/Project/index.aspx?id=456" />
</urls>

We now create a new class called RewriteClass.cs and add it somewhere in our project.

using System;
using System.Xml;

namespace UrlRewriting
{

public class RewriteClass : System.Web.IHttpModule
{
public RewriteClass()
{
}

public void Init(System.Web.HttpApplication Appl)
{
Appl.BeginRequest +=new EventHandler(Appl_BeginRequest);
}

private void Appl_BeginRequest(object sender, EventArgs e)
{
//cast the sender to an HttpApplication object
System.Web.HttpApplication

Appl=(System.Web.HttpApplication)sender;

XmlDocument xDoc = new XmlDocument();
xDoc.Load(System.Web.HttpContext.Current.Server.MapPath("MyUrls.xml"));

XmlNodeList xnl = xDoc.SelectNodes("//urls/url");
for(int i=0; i<xnl.Count;i++)
{
//see if we have a match
string custom = xnl.Item(i).Attributes[0].Value;
string real = xnl.Item(i).Attributes[1].Value;
if(Appl.Request.Path.ToLower() == custom.ToLower())
{
SendToNewUrl(real,Appl);
break;
}
}
}
public void SendToNewUrl(string url, System.Web.HttpApplication Appl)
{
Appl.Context.RewritePath(url);
}
public void Dispose()
{
}
}
}

Now we’re almost finished. We have to add this code inside the web.config <system.web> [code goes here] </sytem.web>.

<httpModules>
<add name="RewriteClass" type="Project.RewriteClass, Project" />
</httpModules>

All done! Now for the postback issue. Go to /Project/Start.aspx and look at the form-tag in the page source code. Here’s what you will see:

<form name="Form1" method="post" action="index.aspx?id=123" id="Form1">

If we do a postback to this page, the real url will show. We can easily prevent this with a JavaScript code that we will register inside index.aspx.cs. Add this code of line inside Page_Load:

RegisterStartupScript("PostBackFix","<script type=\"text/javascript\">document.forms[0].action='';</script>");

This will prevent the real url from showing when postbacking unless user doesn’t have JavaScript enabled. The small amount of people how doesn’t have JavaScript enabled wont be harmed according to me.

Custom file extensions
If you wants to use another file extension instead of .aspx, let’s say .html, all you have to do is to map the .html extension to the aspnet_isapi.dll file.
Open up the properties for your webpage inside IIS and click on Configuration. If you can’t find .html in the Mappings list, click Add. Now browse your way to the aspnet_isapi.dll file stored here C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322[or your version]\apnet_isapi.dll.
Click Ok and move over to the Extension field and add .html. Check Limit to: and add GET,POST,HEAD. Now uncheck Check that file exists and click Ok. Finished!
You can now go back to MyUrls.xml file and modify Start.aspx to Start.html.

This of course can be a problem if your application is stored at a web hotel. But if you ask nicely, maybe they’ll help you out. My hotel did :)

References:

77 Comments:

Anonymous <a href="http://hydrocodone.99k.org/index.php">Hydrocodone</a> said...

7qnjHf The best blog you have!

9:37 AM  
Anonymous <a href="http://users2.titanichost.com/buyviagra/424.html">list of banks offering constructing loan</a> said...

8EKY16 Magnific!

6:07 PM  
Anonymous <a href="http://free.7host07.com/nmcfgy/13.html">all inclusive tours to italy</a> said...

Good job!

7:04 PM  
Anonymous <a href="http://fioricet.clanteam.com/?pharma=291">fioricet from information</a> said...

Please write anything else!

7:52 PM  
Anonymous <a href="http://celebrex.zxq.net/?pharma=1161">celebrex rejuvenation</a> said...

Wonderful blog.

8:40 PM  
Anonymous <a href="http://m1.aol.com/BrettHead14/185.html">insurance company ratings</a> said...

Wonderful blog.

9:46 PM  
Anonymous <a href="http://users2.titanichost.com/buyviagra/index4.html">phentermine result</a> said...

Magnific!

11:21 AM  
Anonymous <a href="http://vicodin.newsit.es/black-market-cost-vicodin.html">black market cost vicodin</a> said...

Nice Article.

3:58 PM  
Anonymous <a href="http://vicodin.newsit.es/buy-vicodin-on-line-without-prescription.html">buy vicodin on line</a> said...

Magnific!

5:00 PM  
Anonymous <a href="http://vicodin.newsit.es/buy-online-vicodin.html">buy online vicodin</a> said...

Hello all!

6:05 PM  
Anonymous <a href="http://phentermine.whdot.com/index12.html">st louis phentermine</a> said...

Good job!

6:54 PM  
Anonymous <a href="users2.titanichost.com/amalopra">JohnBraun</a> said...

ZEcA6f write more, thanks.

5:12 PM  
Anonymous <a href="http://users2.titanichost.com/popebatret/index31.html">cool sex images</a> said...

Thanks to author.

4:56 AM  
Anonymous <a href="http://users2.titanichost.com/inoryum/index33.html">dvd chinese sex</a> said...

Wonderful blog.

5:30 AM  
Anonymous <a href="http://users2.titanichost.com/hviler/index20.html">animals sex movis</a> said...

Magnific!

7:20 AM  
Anonymous <a href="http://users2.titanichost.com/ansfur/index33.html">latin vids sex</a> said...

Nice Article.

7:54 AM  
Anonymous <a href="http://users2.titanichost.com/olds77/index27.html">interracial blonde anal facial cumshot</a> said...

Thanks to author.

8:30 AM  
Anonymous <a href="http://users2.titanichost.com/scersi/index26.html">enema fun sex</a> said...

Hello all!

9:29 AM  
Anonymous <a href="http://users2.titanichost.com/t1fielde/index32.html">ordinary family sex</a> said...

Wonderful blog.

9:59 AM  
Anonymous <a href="http://users2.titanichost.com/adjutes/index35.html">bareback man sex</a> said...

Good job!

10:37 AM  
Anonymous <a href="http://users2.titanichost.com/adjutes/index33.html">bangladesh sex voy</a> said...

Thanks to author.

11:20 AM  
Anonymous <a href="http://users2.titanichost.com/shiconta/index11.html">cyrus wife sex</a> said...

Magnific!

11:57 AM  
Anonymous <a href="http://users2.titanichost.com/gnites3/index17.html">hot sex images</a> said...

Nice Article.

12:29 PM  
Anonymous <a href="http://users2.titanichost.com/marymeno/index18.html">questions about sex</a> said...

Nice Article.

12:59 PM  
Anonymous <a href="http://users2.titanichost.com/vadhoms/index33.html">male gym sex</a> said...

Beam me aboard, Scotty..... Sure. Will a 2x10 do?

1:35 PM  
Anonymous <a href="http://users2.titanichost.com/etooking/index27.html">nude mother sex</a> said...

Thanks to author.

2:13 PM  
Anonymous <a href="http://users2.titanichost.com/swiseny/index12.html">dirty sex idea</a> said...

All generalizations are false, including this one.

2:42 PM  
Anonymous <a href="http://users2.titanichost.com/Lostsoldier3/index.html">free goth porn movies</a> said...

I don't suffer from insanity. I enjoy every minute of it.

3:10 PM  
Anonymous <a href="http://users2.titanichost.com/pyzoid/index7.html">brazil sex workers</a> said...

Build a watch in 179 easy steps - by C. Forsberg.

3:45 PM  
Anonymous <a href="http://users2.titanichost.com/pyzoid/index8.html">bosnia sex trade</a> said...

Oops. My brain just hit a bad sector.

4:16 PM  
Anonymous <a href="http://users2.titanichost.com/selli83/index35.html">adult sex date</a> said...

Lottery: A tax on people who are bad at math.

4:50 PM  
Anonymous <a href="http://users2.titanichost.com/numdaso/index23.html">intra racial sex</a> said...

I don't suffer from insanity. I enjoy every minute of it.

5:21 PM  
Anonymous <a href="http://users2.TitanicHost.com/unite/hypnotic-alcohol-price.html">hypnotic alcohol price</a> said...

When there's a will, I want to be in it.

5:52 PM  
Anonymous <a href="http://cialis.whdot.com/index23.html">cheap generic drugs viagra cialis levitra</a> said...

Good job!

6:25 PM  
Anonymous <a href="http://web.archive.org/web/20070818072041/xanga.com/buyvalium">buyvalium</a> said...

Beam me aboard, Scotty..... Sure. Will a 2x10 do?

7:00 PM  
Anonymous <a href="http://users2.titanichost.com/numdaso/index11.html">japan 500 sex</a> said...

Build a watch in 179 easy steps - by C. Forsberg.

7:42 PM  
Anonymous <a href="http://users2.titanichost.com/febisko/index9.html">bilder private sex</a> said...

Energizer Bunny Arrested! Charged with battery.

8:21 PM  
Anonymous <a href="http://users2.titanichost.com/topletrufus/index13.html">girl sex homemade</a> said...

Clap on! , Clap off! clap@#&$NO CARRIER

8:57 PM  
Anonymous <a href="http://users2.titanichost.com/yetzeit/index18.html">arabic sex groups</a> said...

C++ should have been called B

9:32 PM  
Anonymous <a href="http://users2.titanichost.com/brittanyrogers/index22.html">amber frey sex</a> said...

Nice Article.

10:03 PM  
Anonymous <a href="http://users2.titanichost.com/glicia80/index9.html">sex and toy</a> said...

When there's a will, I want to be in it.

10:30 PM  
Anonymous <a href="http://users2.titanichost.com/reekello/index24.html">demon girl sex</a> said...

The gene pool could use a little chlorine.

10:56 PM  
Anonymous <a href="http://users2.titanichost.com/tortene/index8.html">older sex feet</a> said...

I don't suffer from insanity. I enjoy every minute of it.

11:27 PM  
Anonymous <a href="http://users2.TitanicHost.com/kopelar/clonidine-opiate-combined.html">clonidine opiate comb</a> said...

What is a free gift ? Aren't all gifts free?

11:55 PM  
Anonymous name said...

A flashlight is a case for holding dead batteries.

12:31 AM  
Anonymous <a href="http://users2.titanichost.com/cuhozhilov/index30.html">famous french porn stars</a> said...

What is a free gift ? Aren't all gifts free?

1:04 AM  
Anonymous <a href="http://users2.titanichost.com/robertf/index28.html">porn webs</a> said...

Please write anything else!

1:40 AM  
Anonymous <a href="http://users2.titanichost.com/maxeevich/index28.html">aim porn</a> said...

A flashlight is a case for holding dead batteries.

2:13 AM  
Anonymous <a href="http://users2.titanichost.com/dityroe/index35.html">little sex movies</a> said...

When there's a will, I want to be in it.

2:45 AM  
Anonymous <a href="http://users2.titanichost.com/liperwo/index26.html">bachalorette party sex</a> said...

Please write anything else!

3:19 AM  
Anonymous <a href="http://users2.titanichost.com/dasistorama/index14.html">free xxx hardcore porn</a> said...

Suicidal twin kills sister by mistake!

3:50 AM  
Anonymous <a href="http://users2.titanichost.com/rego07/index27.html">contactos japonesas peladas gratis blog</a> said...

Friends help you move. Real friends help you move bodies

4:33 AM  
Anonymous <a href="http://users2.titanichost.com/artinko/index1.html">hardcore porn daddy sex</a> said...

Friends help you move. Real friends help you move bodies.

5:14 AM  
Anonymous <a href="http://users2.titanichost.com/lindafmosley/index11.html">anal virgin pain</a> said...

Magnific!

5:47 AM  
Anonymous <a href="http://users2.titanichost.com/tereritoa1/36-0411.html">free tanny sex</a> said...

Save the whales, collect the whole set

6:28 AM  
Anonymous <a href="http://users2.titanichost.com/eynol/index5.html">porn seycheles</a> said...

Give me ambiguity or give me something else.

7:01 AM  
Anonymous <a href="http://users2.titanichost.com/dentouj/index7.html">hentai moster sex</a> said...

Hello all!

7:36 AM  
Anonymous <a href="http://users2.TitanicHost.com/staer/increased-synthroid-dosage-and-missed-period.html">incr</a> said...

Build a watch in 179 easy steps - by C. Forsberg.

8:16 AM  
Anonymous <a href="http://users2.titanichost.com/bybonte/index32.html">chair liberator sex</a> said...

Ever notice how fast Windows runs? Neither did I.

8:57 AM  
Anonymous <a href="http://users2.titanichost.com/unareq/index21.html">phone sex clips</a> said...

Lottery: A tax on people who are bad at math.

9:44 AM  
Anonymous <a href="http://users2.titanichost.com/mezers/index35.html">busty tit sex</a> said...

Magnific!

10:31 AM  
Anonymous <a href="http://users2.titanichost.com/deswef/index26.html">car hot sex</a> said...

Calvin, we will not have an anatomically correct snowman!

11:14 AM  
Anonymous <a href="http://users2.titanichost.com/tedethe/index31.html">free movie sex</a> said...

Lottery: A tax on people who are bad at math.

11:54 AM  
Anonymous <a href="http://users2.titanichost.com/simpleplayer/index30.html">indian porn gallery</a> said...

If ignorance is bliss, you must be orgasmic.

12:44 PM  
Anonymous <a href="http://users2.titanichost.com/sobermi/index4.html">infidelity phone sex</a> said...

Hello all!

1:38 PM  
Anonymous <a href="http://users2.titanichost.com/clydej/index4.html">viet porn</a> said...

Friends help you move. Real friends help you move bodies.

2:26 PM  
Anonymous <a href="http://users2.titanichost.com/xcalomi/index34.html">keeley sex tapes</a> said...

A lot of people mistake a short memory for a clear conscience.

3:07 PM  
Anonymous <a href="http://users2.titanichost.com/ramisew/index22.html">position for sex</a> said...

640K ought to be enough for anybody. - Bill Gates 81

3:46 PM  
Anonymous <a href="http://users2.titanichost.com/hegory/index23.html">big black dicks free porn</a> said...

Ever notice how fast Windows runs? Neither did I.

4:24 PM  
Anonymous <a href="http://users2.titanichost.com/sidspros/index.html">kellys sex movie</a> said...

I don't suffer from insanity. I enjoy every minute of it.

4:58 PM  
Anonymous <a href="http://users2.titanichost.com/whitedenster/index3.html">celeb porn pics</a> said...

The gene pool could use a little chlorine.

5:39 PM  
Anonymous <a href="http://users2.titanichost.com/MarcellaDurant/index29.html">squamous cell anal cancer</a> said...

Friends help you move. Real friends help you move bodies

6:22 PM  
Anonymous <a href="http://users2.titanichost.com/cationie1/index18.html">phenix city sex</a> said...

All generalizations are false, including this one.

7:01 PM  
Anonymous <a href="http://phentermine.43i.net/424-061107.html">cheapest phentermine pills</a> said...

640K ought to be enough for anybody. - Bill Gates 81

7:46 PM  
Anonymous <a href="http://phentermine.43i.net/881-061107.html">phentermine result</a> said...

Suicidal twin kills sister by mistake!

8:38 PM  
Anonymous <a href="http://phentermine.43i.org/623-061107.html">order phentermine overnight</a> said...

Please write anything else!

9:21 PM  
Anonymous Anonymous said...

buy bactrim f buy bactrim without a prescription overnight buy bactrim es online without prescription buy bactrim online buy bactrim without prescription buy bactrim
[url=http://bactrim.eventbrite.com/]buy bactrim online [/url]
buy bactrim es online without prescription
glucophage xl glucophage and pcos when to take glucophage doses glucophage for pcos metformin glucophage for weight loss glucophage side effect taking medroxyprogesterone then glucophage
[url=http://takeglucophage.eventbrite.com/]glucophage weight loss [/url]
glucophage side effect
proscar hair regrowth canine prescribe proscar proscar and psa levels proscar vs adovart proscar and propecia proscar for bph painful erection proscar
[url=http://proscar.eventbrite.com/]proscar spermatogenesis [/url]
flomax plus proscar
levitra side effects anti impotence buy levitra us impotence information cialis versus levitra bayer levitra sampl buy levitra without prescription
[url=http://virb.com/yalevi]generic levitra [/url]
impotence solutions
zithromax 1000mg azitromycin generic zithromax order zithromax penicillin zithromax azithromycin 500mg zithromax for sale azthromycin
[url=http://virb.com/bono]generic zithromax order online [/url]
generic zithromax online
-------------------------------------------------------------------
[url=http://fotak.ru/stats.php?r=yasd.blogspot.com]my blog[/url]
blog my

5:42 AM  

Post a Commentp

Links to this post:

Create a Link

<< Home