|
My first ASP.NET 2.0 HTTP Handler sample
by Steve Schofield
download the code
HTTP Handlers are something that will become more popular as IIS 7 is released
in the next version of Windows. This sample isn't really meant to
shock the world but you have to start somewhere. :) I've not done much with
HTTP handlers and wanted a 'Hello world' to reference. These are 'ISAPI-filter' like which provide a lot of power.
Below is my "Hello world" HTTP Handler and it only took about 15 minutes
to do. Enjoy.
- Create a new website project in Visual Studio 2005.
- Create a default document in the root folder
- Add a class file called 'steveschofield.vb' or whatever you want!
- Add this code to your .vb file
Imports Microsoft.VisualBasic
Imports System.Web
Namespace SteveSchofield
Public Class HelloWorld_HTTPHandler : Implements IHttpHandler
Public Sub ProcessRequest(ByVal
Context As HttpContext) Implements IHttpHandler.ProcessRequest
Context.Response.Write("<html><body>Hello World! -- " &
System.DateTime.Now.ToString() & _
"</body></html>")
End Sub
Public ReadOnly Property IsReusable()
As Boolean Implements IHttpHandler.IsReusable
Get
Return True
End Get
End Property
End Class
End Namespace |
- Add a file that will trigger the HTTP Handler, in my project I created a
file called 'HelloWorldHTTPHandler.steveschofield' This is nothing
more than a blank web-form. I supposed it could be something that
is triggered in the Global.aspx or something else but for my sample I kept
it simple.
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="HelloWorldHTTPHandler.steveschofield.vb"
Inherits="HelloWorldHTTPHandler" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
|
- Add an web.config file to your ASP.NET project.
- Add the code in the System.Web portion of the web.config. Notice
the path is to the web-form we created in the previous step. Notice
the Type value references .vb file.
<httpHandlers>
<add verb="*" path="HelloWorldHTTPHandler.steveschofield" type="SteveSchofield.HelloWorld_HTTPHandler"
/>
</httpHandlers> |
Compile and run the default document, (Oh, forgot to add put a link to the
blank HelloWorldHTTPHandler.steveschofield webform created earlier) or just
run the HelloWorldHTTPHandler.steveschofield page directly. Your choice.
The results! This would be written out in the browser.
|
<html><body>Hello World! -- 1/1/2006 7:45:52 PM</body></html> |
|
|