Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Handling requests without an extension

9 Sep 2008 0  
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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here