How To Create an ASP.NET HTTP Handler by Using Visual Basic .NET (307997)
The information in this article applies to:
- Microsoft ASP.NET (included with the .NET Framework 1.1)
- Microsoft ASP.NET (included with the .NET Framework) 1.0
- Microsoft Visual Basic .NET (2003)
- Microsoft Visual Basic .NET (2002)
This article was previously published under Q307997 For a Microsoft Visual C# .NET version of this
article, see
308001. IN THIS TASKSUMMARY This step-by-step article demonstrates how to use Microsoft Visual
Basic .NET to create a simple, custom HTTP handler. This article demonstrates
how to create, deploy, and configure the handler.
back to the top
Implement the Handler- Start
Microsoft Visual Studio .NET.
- Create a new Class Library project
by using Visual Basic .NET, and then name the project MyHandler.
- Add
a reference to the System.Web.dll assembly.
- Add the following code to import the System.Web namespace:
Imports System.Web
- Rename the class SyncHandler.vb, and then change the class
definition to reflect this.
- Implement the IHttpHandler interface. Your class definition should appear as follows:
Public Class SyncHandler
Implements IHttpHandler
- Implement the IsReusable property and the ProcessRequest method of the IHttpHandler interface. Because this is a synchronous handler, return False for the IsReusable property so that the handler is not pooled.
Public ReadOnly Property IsReusable() As Boolean _
Implements IHttpHandler.IsReusable
Get
Return False
End Get
End Property
Public Sub ProcessRequest(ByVal context As HttpContext) _
Implements IHttpHandler.ProcessRequest
context.Response.Write("Hello from custom handler.")
End Sub
- Compile the project.
Note: If you want your handler to have access to session data, then
your class must implement the IRequiresSessionState interface in addition to IHttpHandler. IRequiresSessionState has no methods or properties.
It merely designates that your handler uses session
data.
back to the top
Deploy the Handler- Create a new directory named Handler under the
C:\Inetpub\Wwwroot directory.
- Create a subdirectory named Bin in the newly created
Handler directory. The resultant path is
C:\Inetpub\Wwwroot\Handler\Bin.
- Copy MyHandler.dll from your project's Bin directory to the
C:\Inetpub\Wwwroot\Handler\Bin directory.
- Follow these steps to mark the new Handler directory as a
Web application:
- In
Microsoft Windows 2000 and in Microsoft Windows XP, start
Internet Services Manager.
In Microsoft Windows Server 2003, start Internet Information Services (IIS) Manager.
- Right-click on the Handler directory, and then click Properties.
- On the Directory tab, click Create.
- Follow these steps to create an application mapping for the
handler. For this handler, create a mapping to the Aspnet_isapi.dll file for
the *.sync extension. Whenever a .sync file is requested, the request is routed
to ASP.NET, and ASP.NET executes the code in the handler.
- Right-click on the Handler Web application, and then
click Properties.
- On the Directory tab, click Configuration.
- Click Add to add a new mapping.
- In the Executable text box, type the following path:
C:\WINNT\Microsoft.NET\Framework\<version#>\Aspnet_isapi.dll - In the Extension text box, type .sync.
- In
Windows 2000 and in Windows XP, make
sure that the Check that file exists check box is cleared, and
then click OK to close the Add/Edit Application Extension Mapping dialog box.
In Windows Server 2003, make sure that the Verify that file
exists check box is cleared, and then click OK to close the Add/Edit Application Extension Mapping dialog box.
- Click OK to close the Application Configuration and the Handler Properties dialog boxes.
- Close Internet Services Manager.
back to the top
Configure the System- In the C:\Inetpub\Wwwroot\Handler directory, create a new
file named Web.config.
- Add the following code to Web.config:
<configuration>
<system.web>
<httpHandlers>
<add verb="*" path="*.sync" type="MyHandler.SyncHandler, MyHandler" />
</httpHandlers>
</system.web>
</configuration>
In the verb="*" attribute, we instruct the handler to process a request that uses
any verb (for example, POST, HEAD, GET, and so on). If you want this handler to
process only the POST request, change this to verb="POST".
In the path="*.sync" attribute, we instruct the handler to process any incoming
requests for files with the .sync extension.
In the type="MyHandler.SyncHander, MyHandler" attribute, we instruct the handler that processes the request to
implement in the MyHandler.SyncHandler namespace, and this class resides in the MyHandler assembly.
back to the top
Test the Module To test a handler, a page does not need to exist in the file
system. For example, request the Default.sync file in the Handler Web
application (http://< ComputerName>/Handler/Default.sync). You should receive the following
results:
Hello from custom handler.
back to the top
REFERENCESFor additional information about HTTP
handlers, click the article number below to view the article in the Microsoft
Knowledge Base: 307985 INFO: ASP.NET HTTP Modules and HTTP Handlers Overview
back to the top
Modification Type: | Minor | Last Reviewed: | 7/15/2004 |
---|
Keywords: | kbConfig kbHOWTOmaster KbhttpHandlers kbHttpRuntime kbweb KB307997 kbAudDeveloper |
---|
|