Microsoft KB Archive/308001

= How To Create an ASP.NET HTTP Handler by Using Visual C# .NET =

Article ID: 308001

Article Last Modified on 7/15/2004

-

APPLIES TO


 * Microsoft ASP.NET 1.1
 * Microsoft ASP.NET 1.0
 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft Visual C# .NET 2002 Standard Edition

-



This article was previously published under Q308001



For a Microsoft Visual Basic .NET version of this article, see 307997.

IN THIS TASK

 * SUMMARY
 * Implement the Handler
 * Deploy the Handler
 * Configure the System
 * Test the Module
 * REFERENCES



This step-by-step article demonstrates how to use Visual C# .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
 Open Microsoft Visual Studio .NET. In Visual C# .NET, create a new Class Library project named MyHandler. Set a reference to the System.Web.dll assembly.  Add the following directive to the class: using System.Web;  Rename the class SyncHandler.cs, and then change the class definition to reflect this.  Implement the IHttpHandler interface. Your class definition should appear as follows: public class SyncHandler : 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 bool IsReusable {  get {return false;} }

public void ProcessRequest(HttpContext context) {  context.Response.Write(&quot;Hello from custom handler.&quot;); }                   </li> Compile the project.</li></ol>

back to the top

Deploy the Handler
<ol> Create a new directory named Handler under the C:\Inetpub\Wwwroot directory.</li> Create a subdirectory named Bin in the newly created Handler directory. The resultant path is C:\Inetpub\Wwwroot\Handler\Bin.</li> Copy MyHandler.dll from your project's Bin\Debug directory to the C:\Inetpub\Wwwroot\Handler\Bin directory.</li> Follow these steps to mark the new Handler directory as a Web application: <ol style="list-style-type: lower-alpha;"> Open Internet Services Manager.</li> Right-click the Handler directory, and then click Properties.</li> On the Directory tab, click Create.</li></ol> </li> 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. <ol style="list-style-type: lower-alpha;"> Right-click on the Handler Web application, and then click Properties.</li> On the Directory tab, click Configuration.</li> Click Add to add a new mapping.</li> In the Executable text box, type the following path: Microsoft Windows 2000:

C:\WINNT\Microsoft.NET\Framework\<version#>\Aspnet_isapi.dll

Microsoft Windows XP:

C:\WINDOWS\Microsoft.NET\Framework\<version#>\Aspnet_isapi.dll

</li> In the Extension text box, type .sync .</li> 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.</li> Click OK to close the Application Configuration and the Handler Properties dialog boxes.</li></ol> </li> <li>Close Internet Services Manager.</li></ol>

back to the top

Configure the System
<ol> <li>In the C:\Inetpub\Wwwroot\Handler directory, create a new file named Web.config.</li> <li> Add the following code to Web.config: <system.web> <httpHandlers> <add verb=&quot;*&quot; path=&quot;*.sync&quot; type=&quot;MyHandler.SyncHandler, MyHandler&quot; /> </httpHandlers> </system.web> In the verb=&quot;*&quot; 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=&quot;POST&quot;.

In the path=&quot;*.sync&quot; attribute, we instruct the handler to process any incoming requests for files with the .sync extension.

In the type=&quot;MyHandler.SyncHandler, MyHandler&quot; attribute, we instruct the handler that processes the request to implement in the MyHandler.SyncHandler namespace, and this class resides in the MyHandler assembly. </li></ol>

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

<div class="references_section">