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:
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:
7qnjHf The best blog you have!
8EKY16 Magnific!
Good job!
Please write anything else!
Wonderful blog.
Wonderful blog.
Magnific!
Nice Article.
Magnific!
Hello all!
Good job!
ZEcA6f write more, thanks.
Thanks to author.
Wonderful blog.
Magnific!
Nice Article.
Thanks to author.
Hello all!
Wonderful blog.
Good job!
Thanks to author.
Magnific!
Nice Article.
Nice Article.
Beam me aboard, Scotty..... Sure. Will a 2x10 do?
Thanks to author.
All generalizations are false, including this one.
I don't suffer from insanity. I enjoy every minute of it.
Build a watch in 179 easy steps - by C. Forsberg.
Oops. My brain just hit a bad sector.
Lottery: A tax on people who are bad at math.
I don't suffer from insanity. I enjoy every minute of it.
When there's a will, I want to be in it.
Good job!
Beam me aboard, Scotty..... Sure. Will a 2x10 do?
Build a watch in 179 easy steps - by C. Forsberg.
Energizer Bunny Arrested! Charged with battery.
Clap on! , Clap off! clap@#&$NO CARRIER
C++ should have been called B
Nice Article.
When there's a will, I want to be in it.
The gene pool could use a little chlorine.
I don't suffer from insanity. I enjoy every minute of it.
What is a free gift ? Aren't all gifts free?
A flashlight is a case for holding dead batteries.
What is a free gift ? Aren't all gifts free?
Please write anything else!
A flashlight is a case for holding dead batteries.
When there's a will, I want to be in it.
Please write anything else!
Suicidal twin kills sister by mistake!
Friends help you move. Real friends help you move bodies
Friends help you move. Real friends help you move bodies.
Magnific!
Save the whales, collect the whole set
Give me ambiguity or give me something else.
Hello all!
Build a watch in 179 easy steps - by C. Forsberg.
Ever notice how fast Windows runs? Neither did I.
Lottery: A tax on people who are bad at math.
Magnific!
Calvin, we will not have an anatomically correct snowman!
Lottery: A tax on people who are bad at math.
If ignorance is bliss, you must be orgasmic.
Hello all!
Friends help you move. Real friends help you move bodies.
A lot of people mistake a short memory for a clear conscience.
640K ought to be enough for anybody. - Bill Gates 81
Ever notice how fast Windows runs? Neither did I.
I don't suffer from insanity. I enjoy every minute of it.
The gene pool could use a little chlorine.
Friends help you move. Real friends help you move bodies
All generalizations are false, including this one.
640K ought to be enough for anybody. - Bill Gates 81
Suicidal twin kills sister by mistake!
Please write anything else!
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
Post a Commentp
Links to this post:
Create a Link
<< Home