Click here to Skip to main content
Click here to Skip to main content

Handling requests without an extension

, 9 Sep 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
How to handle requests without an extension.

Introduction

This article explains how to handle web requests in which there is no extension added. It's not a URL-Rewrite, but something like that.

Background

We will use an IIS property and some code to trap a URL without an extension. Assume we need to trap the following URLs or we want to make them working:

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

Using the Code

As I mentioned above, we want to handle extension-less URLs and give the appropriate results. In our example, the URL does not contain the extension, and on such a request, I have to display the profile of a particular user, i.e., the profile of Imran and the profile of Armaan.

For this, we have to do two things: first, configure IIS to redirect such a request to an ASPX page, and write code to grab the URL and redirect to the requested page.

Let's first do the IIS configuration part.

In general, when you send a request, it looks for the directory that you asked for; if found, then we will see the type of application and the default page. In our case, we are looking for Imran or Armaan; as the directory does not exist, IIS will give you a 404 error and show a default 404 error page, which you can see in the following image:

IIsSetting1.JPG

Now, our first change in the IIS side. We have to change the default 404 page to our own page. To do this, we need to change the 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 a 404 error occurs in our application, it will get redirected to Handle404.axpx, in which we write some code to achieve 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 is such a redirection, the 404 URL is included into the querystring as follows:

http://localhost/MySite/Handle404.aspx?404;http://MySite/imran

We grab the 404 URL and parse it out and get the username, which is imran or armaan, and make a Server.Transfer so the URL will not be affected and the page will be displayed.

There are lots of cases when we will have a 404 which redirects to the Handle404 page, and we have to make sure that the code only handles the request to show a profile.

Problems or Precaution

You have to be sure about the directory structure. I have the username as Admin, so the URL of my profile will be http://MySite/Admin/. But what if you have Admin as a folder in your website structure?? It will go to the Admin directory and look for the default page. Moreover, if you apply the Authentication mode and the Admin directory needs authentication, it's redirected to the login page or ask for credentials.

So you have to be sure about the username and the directory structure to achieve this.

License

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

Share

About the Author

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

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150327.1 | Last Updated 10 Sep 2008
Article Copyright 2008 by bhadeliaimran
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid