Click here to Skip to main content
15,893,486 members
Articles / Web Development / ASP.NET
Article

Httphandlers ASP.NET 2.0

Rate me:
Please Sign up or sign in to vote.
3.39/5 (13 votes)
27 Jun 2007CPOL2 min read 91K   3.6K   47   6
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
India India
ASP.NET developer working in India.

Comments and Discussions

 
QuestionCan I use HttpHandler to transfer data to aspx page Pin
Ravi Sant6-Apr-11 19:03
Ravi Sant6-Apr-11 19:03 
GeneralGood Article to start Pin
satalaj13-Sep-07 23:26
satalaj13-Sep-07 23:26 
QuestionGreat idea, but can it rewrite the file extension to .html? Pin
bdykes3-Jul-07 21:56
bdykes3-Jul-07 21:56 
AnswerRe: Great idea, but can it rewrite the file extension to .html? Pin
PrashantRishu4-Jul-07 19:31
PrashantRishu4-Jul-07 19:31 
GeneralRe: Great idea, but can it rewrite the file extension to .html? Pin
bdykes7-Jul-07 10:35
bdykes7-Jul-07 10:35 
GeneralRe: Great idea, but can it rewrite the file extension to .html? Pin
PrashantRishu24-Jul-07 22:43
PrashantRishu24-Jul-07 22:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.