Search

Sep 9, 2008

Extended URL rewriting

I was having one requirement for my .net application. In which I want to parse the following url. Handling request with out extension.

http://mysite.com/imran/
http://mysite.com/armaan/

For these url we can't user ASP.NET URL Re-Writing, as there is no aspx extension added to this url.

When you found such request and its not exists then IIS will give 404 error, but we need to parse such url and do some validation and give them proper output. In this case lets say I would like to see the profile of user imran, then I make request to http://mysite.com/imran/ which gives me the profile of user imran.

This is the normal requirement, we can do it easily in php and java. But in .net this is bit tricky to get such functionality working.

If we use http handler, then also we are not able to identify the request as it has no extension. As its is not reaching to the application, IIS itself reject this request, no global.ascx or web.config

I found very easy solution for this solution. This requires some setting on virtual directory and code in our system to handle such request.

As I mention eariler when you send request to http://mysite.com/imran/ which not exist; IIS will raise 404 error, what we do is, we trap this error and get the requested page or url and do some work and give the result.

IIS Setting
In IIS we have to change default 404 error handling to aspx page, bye default 404 page is C:\WINDOWS\help\iisHelp\common\404b.htm with Message Type as File. See the image



We need to change this to forward all such request to our application, once we got control in aspx page then we can do anything on such request.

For this we need to change Message Type, set it to URL and the URL will be /MySite/Handle404.aspx. See the image



Code
We have to write few lines of code in Handle404.aspx to handel this request.

protected override void OnInit(EventArgs e)
{

if (Request.Url.ToString().Contains("404;"))
{
string[] urlInfo404 = Request.Url.Query.ToString().Split(';');

if (urlInfo404.Length > 1)
{
string strRequestUrl = urlInfo404[1].Replace(":" + Request.Url.Port + "/", "/");

if (!strRequestUrl.EndsWith("/"))
{
strRequestUrl = strRequestUrl.Insert(strRequestUrl.Length, "/");
Response.Redirect(strRequestUrl);
}
string[] urlInfoFavAddress = strRequestUrl.Split('/');
string strUser = urlInfoFavAddress[urlInfoFavAddress.Length - 2];

Server.Transfer(string.Concat("~/Profile.aspx?usr=", strUser));

}
}
base.OnInit(e);
}
Here we are just grabbing the trailing string, in this case its imran or armaan.

Once I get the key its get redirected to Profile page, as its Server.Transfer which does not change the url.

3 comments:

Anonymous said...

Great !!!

This is work only if we have IIS control.

if not then what ...

do u have for with out IIS setting ???

Imran said...

No, wiht out IIS its not possible, I think you are looking for this functionality on the web hosting site. But I don't think this will help in that case.

Anonymous said...

Hi,

Great!!! excellent....
It has served my all purpose.

Thanks,

Ranadip