[VB.NET][TUT] Creating And Implementing HTTP Handlers

Discussion in 'Programming General' started by Kyle_Undefined, May 24, 2012.

[VB.NET][TUT] Creating And Implementing HTTP Handlers
  1. Unread #1 - May 24, 2012 at 11:30 PM
  2. Kyle_Undefined
    Joined:
    Feb 9, 2012
    Posts:
    53
    Referrals:
    0
    Sythe Gold:
    0

    Kyle_Undefined Member

    [VB.NET][TUT] Creating And Implementing HTTP Handlers

    Before reading this tutorial, please keep in mind that this is a few years old, and can be upgraded to be database driven (Which I've done, just don't have time to write a new tutorial on the changes. Just re-purposing good code :)). Also, sorry for the image quality.

    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.

    1. Open up your developer tool of choice. Mine is Visual Studio 2005

    [​IMG]

    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.

    [​IMG]

    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

    [​IMG]

    5. Then, Implement System.Web.IHTTPHandler

    [​IMG]

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

    [​IMG]

    7. Make a Sub that processes the HTTP

    [​IMG]

    8. Next we Dim two variables to hold the HTTP Request and the Response

    [​IMG]

    9. Now, we Dim two more variables to hold the RootPath and the filename after stripping out the RootPath and the trailing "/"

    [​IMG]

    *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

    [​IMG]

    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.

    [​IMG]

    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.

    [​IMG]

    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.

    [​IMG]

    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.
    Code:
    <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!

    Originally posted by myself over at DreamInCode.net as well.
     
  3. Unread #2 - May 28, 2012 at 11:28 AM
  4. sm321
    Joined:
    Nov 2, 2011
    Posts:
    2,514
    Referrals:
    1
    Sythe Gold:
    0

    sm321 Grand Master

    [VB.NET][TUT] Creating And Implementing HTTP Handlers

    Good tutorial :) It explains everything step by step, along with what each thing does. Are you self taught at this, as it seems very advanced. Also, how long have you been doing this?
     
  5. Unread #3 - Jun 8, 2012 at 1:39 AM
  6. Kyle_Undefined
    Joined:
    Feb 9, 2012
    Posts:
    53
    Referrals:
    0
    Sythe Gold:
    0

    Kyle_Undefined Member

    [VB.NET][TUT] Creating And Implementing HTTP Handlers

    Thanks! :)

    I am self taught, I have learned from code written by others and have read many books. I love it, and have been doing this around 4 years now.

    I have done this with PHP as well, with a MySQL database holding all the pages. Might make a tutorial for that someday.
     
< [TUT][VB.NET] Multithreading made painfully easy, in 4 steps | RuneScape Client v1.0 [Using VB.NET 2010] >

Users viewing this thread
1 guest


 
 
Adblock breaks this site