Click here to Skip to main content
6,594,088 members and growing! (13,775 online)
Email Password   helpLost your password?
Web Development » ASP.NET » Howto     Intermediate License: The Code Project Open License (CPOL)

Handling request with out extension

By bhadeliaimran

Handling request with out extension
C#.NET 1.1, .NET 2.0, .NET 3.0, .NET 3.5, ASP.NET
Posted:9 Sep 2008
Views:3,492
Bookmarked:4 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
3 votes for this article.
Popularity: 1.24 Rating: 2.60 out of 5
1 vote, 33.3%
1

2
1 vote, 33.3%
3
1 vote, 33.3%
4

5

Introduction

How to handle the request in which there is no extension added. It's not URL-Rewrite, but kind ot it.

Background

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.

Using the code

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.

IIsSetting1.JPG

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.

IIsSetting2.JPG

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.

Problems or Precaution

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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

bhadeliaimran


Member
Profiles : Code Project, ASP.NET Forums
Blog : Knowledgebase World
Current Company : Softwebsolution INC
User Group : Ahmedabad SQLServer UserGroup
Other : Microsoft Certified Technology Specialist (MCTS)

Occupation: Team Leader
Company: Softweb Solutions
Location: India India

Other popular ASP.NET articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
  (Refresh) 
-- There are no messages in this forum --

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 9 Sep 2008
Editor: Deeksha Shenoy
Copyright 2008 by bhadeliaimran
Everything else Copyright © CodeProject, 1999-2009
Web20 | Advertise on the Code Project