![]() |
Web Development »
ASP.NET »
Howto
Intermediate
License: The Code Project Open License (CPOL)
Handling request with out extensionBy bhadeliaimranHandling request with out extension |
C#.NET1.1, .NET2.0, .NET3.0, .NET3.5, ASP.NET
|
||||||||
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
How to handle the request in which there is no extension added. It's not URL-Rewrite, but kind ot it.
We will use IIS property and some code to trap the url with out extension. We need to trap following urls or we want to make them working.
As I mention above, we want to handle the extension less url and give the appropriate result. In our example url does not contains the extension and on that request I have to display the profile of perticular user; ie profile of Imran and profile of Armaan.
For this we have to do two thing, first configure the IIS to redirect such request to aspx page, and the code to grab url and redirect to the requested page.
Let's first do IIS configuration part.
In general when you send request it looking for the directory that you ased for, if found then will see the type of application and the default page. In our case we are looking for imran or armaan as directory which is not exist, so IIS will give you 404 error and show default 404 error page. Which you can see in following image.
Now our first change at IIS side. We have to change default 404 page to our own page. To do this we need to change Message Type value to URL and give the URL of our application. We will redirect it to /MySite/Handle404.aspx.
By changing this we can have control in our aspx page, so whenever 404 error occure in our appliction it will get redirected to Handle404.axpx, in which we write some code to achive our goal.
Let's check the code part of Handle404.aspx.
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("~/EditProfile.aspx?usr=", strUser));
}
}
base.OnInit(e);
}
When there such redirection, the 404 url is included into querystring as following.
http://localhost/MySite/Handle404.aspx?404;http://MySite/imran
We grab 404 URL and parsed it out and get the username, which is imran or armaan and make a Server.Transfer so url will not affected and page will be Profile
There is lots of cases when we are haivng 404 which redirects to Handle404 page, and we have to take care that this code is only handles the request to show profile not other.
You have to be sure about the directory structure, like if I am having username as Admin, so the url of my profile will be http://MySite/Admin/ but what if you are having Admin as folder in your web site structure?? It will goes to Admin directory and looking for default page, moreover if you applied Authentication mode, and Admin directory needs authenticateion its redirects to login page or ask for credentials.
So you have to be sure about the username and the directory structure to achive this.
General
News
Question
Answer
Joke
Rant
Admin
Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads.
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 9 Sep 2008 Editor: Deeksha Shenoy |
Copyright 2008 by bhadeliaimran Everything else Copyright © CodeProject, 1999-2010 Web17 | Advertise on the Code Project |