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

Httphandlers ASP.NET 2.0

, 27 Jun 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
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)

Share

About the Author

PrashantRishu
Web Developer
India India
ASP.NET developer working in India.

Comments and Discussions

 
QuestionCan I use HttpHandler to transfer data to aspx page PinmemberRaviSant6-Apr-11 20:03 
GeneralGood Article to start Pinmembersatalaj14-Sep-07 0:26 
QuestionGreat idea, but can it rewrite the file extension to .html? Pinmemberbdykes3-Jul-07 22:56 
AnswerRe: Great idea, but can it rewrite the file extension to .html? PinmemberPrashantRishu4-Jul-07 20:31 
GeneralRe: Great idea, but can it rewrite the file extension to .html? Pinmemberbdykes7-Jul-07 11:35 
GeneralRe: Great idea, but can it rewrite the file extension to .html? PinmemberPrashantRishu24-Jul-07 23:43 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141223.1 | Last Updated 27 Jun 2007
Article Copyright 2007 by PrashantRishu
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid