Page 1 of 1

Creating and Implementing HTTP Handlers This tutorial is written in VB.NET

#1 CamoDeveloper  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 20
  • View blog
  • Posts: 250
  • Joined: 12-June 09

Posted 18 June 2009 - 11:26 PM

Hi,

My name is CamoDeveloper and I'll be your guide through this powerful tool called HTTP Handlers. For those of you asking what a HH(HTTP Handler) is I'll explain. HTTP handlers are the .NET components that implement the System.Web.IHttpHandler interface. Any class that implements the IHttpHandler interface can act as a target for the incoming HTTP requests. Still don't understand? Head on over to the MSDN section for better clarity. Now, for those that didn't see or read the description, this tutorial is written in VB.NET. Wanted to let you know so I wouldn't waste your time.

So, lets begin.
  • Open up your developer tool of choice. Mine is Visual Studio 2005 Posted Image

  • Open up the web app you're going to apply the Handler to

  • Right click in the solution explorer and lick on " Add New Item " and click on the Generic Handler Template. I'm going to name it HTTPHandler, but you can name it anything you want.
    Posted Image
    It's going to come with some code already in there, just clear it all out

  • The first thing you're going to do is Import System.IO Posted Image

  • Then, Implement System.Web.IHTTPHandler Posted Image

  • Next create a Readonly Property to keep the HTTPHandler from using the same URL more than once.
    Posted Image

  • Make a Sub that processes the HTTP
    Posted Image

  • Next we Dim two variables to hold the HTTP Request and the Response
    Posted Image

  • Now, we Dim two more variables to hold the RootPath and the filename after stripping out the RootPath and the trailing "/"
    Posted Image
    *NOTE* : The RootPath can be named in code, but I like to take it out so I don't have to re-compile if I have to change it

  • Then, Dim three more variables. One as an instance of the IHTTPHandler and two as strings
    Posted Image

  • Ok, ok. I know what you're probably thinking. When is the important code going to come up? Well now that we got all of the variables out of the way, here it comes. So, we have all of our variables defined and set to values or instances of the IHTTPHandler, what do we do with them? Well, first we check to see if the requested page exists, and if it does we set our rewriteURL variable to the path followed by the page name. And if it doesn't, well that's where you come in hand. I use a default-content page that has the layout of the site but with no content in it. That way if the user types in the url wrong they will have the links to get to where they wanted to go. After the check is all done, we rewrite the URL. Finally, we Process the request and send the user on their way.
    Posted Image

  • But wait, we're not done yet. We have to create the RewriteOldURL function that isn't defined. All this does is remove everything up to the last "/" leaving the file name.
    Posted Image

    Now that the code part is done, here comes the easy part. Calling the HTTPHandler class. Just put it in the web.config file in the <configuration> tags.
    Posted Image
    In the above picture I put YourSiteName.WebApp but I'm sure for the most part you will need to use YourSiteNameWebApp. I went into the Properties of the WebApp and put a dot between the Solution name and the WebApp.

    If you want to declare the RootPath in the web.config like I have, just add a key in the <appsettings> tag.
       <appsettings>
    	<add key="RootPath" value="/YourWebAppFolder" />
       </appsettings>
       
       <!-- Example
    	  <add key="RootPath" value="/ACFConsulting" />
    	-->
      
    

Please, if you have any questions or comments please feel free to post them here. I hope to write some more tutorials soon. I plan on writing a tutorial on speeding up SQL select statements as well.

Hope you enjoyed it!

~Camo

Is This A Good Question/Topic? 0
  • +

Replies To: Creating and Implementing HTTP Handlers

#2 zseven  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 29-July 09

Posted 29 July 2009 - 07:57 AM

Hi Camo,
tx for your tutorial, but i haven't some problem and it doesn't works.

So, at first, sorry for my english :)
I'will try to explain which is my problem.

I have follow your guide, but i think that the problem is on the web.config

so in httphandler i've wrote:

<add verb="*" path="*.aspx" type="HTTPHandler"/>

or
<add verb="*" path="*.aspx" type="storevioletta.HTTPHandler"/>

but it doesn't works.

You say that i have to wrote the name of the site. this name is the folder where the site is located?
Because i see the website on the localhost only.

So i hope that you can help me.

Tx a lot
Was This Post Helpful? 0
  • +
  • -

#3 CamoDeveloper  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 20
  • View blog
  • Posts: 250
  • Joined: 12-June 09

Posted 30 July 2009 - 08:20 PM

View Postzseven, on 29 Jul, 2009 - 06:57 AM, said:

Hi Camo,
tx for your tutorial, but i haven't some problem and it doesn't works.

So, at first, sorry for my english :)
I'will try to explain which is my problem.

I have follow your guide, but i think that the problem is on the web.config

so in httphandler i've wrote:

<add verb="*" path="*.aspx" type="HTTPHandler"/>

or
<add verb="*" path="*.aspx" type="storevioletta.HTTPHandler"/>

but it doesn't works.

You say that i have to wrote the name of the site. this name is the folder where the site is located?
Because i see the website on the localhost only.

So i hope that you can help me.

Tx a lot


Thanks for using my tutorial. To answer your question, the name is whatever you named your project. It's not the Solution name but what you put into the Name field when creating the project. Lets say I create a brand new project. I'm going to name my Solution : MyProjectSolution. The Name is going to be MyProjectName. In the web.config for the HTTP Handler I'm going to use my project name to denote what class to use. It would be <add verb="*" path="*.aspx" type="MyProjectName.HTTPHandler" />

I hope this explains is better. :)

~Camo
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1