PDA

View Full Version : [TUT] HTTP Handlers



Kyle Undefined
10-28-2009, 08:53 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 (http://msdn.microsoft.com/en-us/library/ms227675%28VS.80%29.aspx) 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.

1. Open up your developer tool of choice. Mine is Visual Studio 2005 http://img.photobucket.com/albums/v489/LilDukey/open.jpg

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

3. 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.
http://img.photobucket.com/albums/v489/LilDukey/choose.jpg
It's going to come with some code already in there, just clear it all out

4. The first thing you're going to do is Import System.IO http://img.photobucket.com/albums/v489/LilDukey/systemio.jpg

5. Then, Implement System.Web.IHTTPHandler http://img.photobucket.com/albums/v489/LilDukey/implements.jpg

6. Next create a Readonly Property to keep the HTTPHandler from using the same URL more than once.
http://img.photobucket.com/albums/v489/LilDukey/property.jpg

7. Make a Sub that processes the HTTP
http://img.photobucket.com/albums/v489/LilDukey/processrequest.jpg

8. Next we Dim two variables to hold the HTTP Request and the Response
http://img.photobucket.com/albums/v489/LilDukey/dim.jpg

9. Now, we Dim two more variables to hold the RootPath and the filename after stripping out the RootPath and the trailing "/"
http://img.photobucket.com/albums/v489/LilDukey/dimrp.jpg
*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

10. Then, Dim three more variables. One as an instance of the IHTTPHandler and two as strings
http://img.photobucket.com/albums/v489/LilDukey/dimhttp.jpg

11. 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.
http://img.photobucket.com/albums/v489/LilDukey/innerworking.jpg

12. 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.
http://img.photobucket.com/albums/v489/LilDukey/rewriteurl.jpg

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.
http://img.photobucket.com/albums/v489/LilDukey/webconfig.jpg
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="/ListenToMeRawr" />
-->



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

Ranger135xp
10-31-2009, 12:44 AM
:( I can't use ASP on my host.

Actually I don't even know where to begin with ASP or .NET.

I prefer good ole PHP and MySQL :p

EDIT:\\ AHHH CRAP! My hundredth post and I couldn't make a celebratory topic...