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

Httphandlers ASP.NET 2.0

0.00/5 (No votes)
27 Jun 2007 1  
This tutorial describes a simple Httphandlers ASP.NET in 2.0

Introduction

Most of the websites are dynamic and their Urls are difficult to remember. For a general user, its very difficult to remember Urls like http://www.himachalsoft.com/userhome.aspx?catid=2&userid=34. However, a url like http://www.himachalsoft.com/prashant.aspx is very easy to remember. .NET provide us functionality to create user friendly (easy to remember) Urls. Besides, if someone searches for prashant on google or any other search engine, chances of reaching the above url is easier. Lets see how.

Background

Prior to .NET, the only way to create friendly urls was to use ISAPI extensions developed usually in C++. ISAPI acts as a filter between your webserver and the client. Each time a client makes a request to the server, it passes through the filter. Creating ISAPI filter meant knowledge of c++ or required language. Moreover, users host websites on third party web servers where you are not allowed to install your components (though some webhosting companies allow this) or you must have dedicated web server. In .NET you have full support to do what ISAPI does and you can also achieve many tasks without configuring your webserver for httphandlers.

Using the code

Let us assume that you have a folder users in your website. This folder has a page default.aspx in it. Your website allows users to register. Each registered user is provided with a url which he can share with his friends, blogs etc. e.g http://www.himachalsoft.com/prashant.aspx. In reality there is no file as prashant.aspx. To achieve this "magic", lets create a httphandler first which will do the trick. Create a new class project. Class UrlHandler implements IhttpHandler interface and implements two methods. IsReusable() and ProcessRequest(). The code in process request checks that if current request contains folder name "Users" in it, then retrieve the string after the folder name and remove .aspx extension to get username. Then it displays the user name. In real scenario, the user name can be looked into database, verified and load user specific data.

Create a new website. Add reference of handler dll created above. Add a folder "Users" in it. Add entry to your webconfig as specified below. Now if you try to open a page e.g. http://www.himachalsoft.com/users/prashant.aspx, you will receive welcome prashant as output. However, in reality such page does not exist. You can build upon this example to verify the username (or product name) from database.

using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
namespace MyHandler
{
    class UrlHandler : IHttpHandler
    {
        #region IHttpHandler Members
        public bool IsReusable
        {
            get { return false; }
        }
        public void ProcessRequest(HttpContext context)
        {
            string vPath = context.Request.RawUrl;
        //The hard coded value for path can be put in config file to create rules

        int vIndexOfFolder = vPath.IndexOf("/users/", StringComparison.OrdinalIgnoreCase);
            if (vIndexOfFolder > 0)
            {
            string vUserName = vPath.Substring(vIndexOfFolder + 7);
            //remove .aspx extension

            vUserName = vUserName.Substring(0,vUserName.Length - 5);
            context.Response.Write("Welcome " & vUserName);
            }
        }
        #endregion
    }
}

//Webconfig

 <httpHandlers >
   <add verb="*" path="users/*.aspx" type="MyHandler.UrlHandler,MyHandler" />
  </httpHandlers>

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