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
- 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.
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
- Then, Implement System.Web.IHTTPHandler
- Next create a Readonly Property to keep the HTTPHandler from using the same URL more than once.
- Make a Sub that processes the HTTP
- Next we Dim two variables to hold the HTTP Request and the Response
- Now, we Dim two more variables to hold the RootPath and the filename after stripping out the RootPath and the trailing "/"
*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
- 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.
- 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.
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.
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!