This topic describes the code of an HTTP handler that performs synchronous request processing on resources whose URLs end with. sample in an ASP. NET application. This sample code illustrates the following:
Code of an HTTP handler class. This class must be implemented Method and Attribute.
Register the handler in the Web. config file and map the. sample file extension to the elements required by the handler.
How to map the. sample file extension to ASP. NET in Internet Information Service (IIS.
Note: |
After you change the configuration file to include a reference to the new handler, ASP. NET Development Server processes requests for new resources. To enable IIS to process this request, see the following procedure. |
When a user requests a resource whose URL ends with. sample, the Web server forwards the request to ASP. NET. ASP. NET then calls the HTTP processing program, and the latter returns a response. The response is dynamically created by the handler; there is no need for a file with the file extension. sample. For more information about how ASP. NET interacts with Web servers, see ASP. NET lifecycle.
Create a custom HelloWorldHandler HTTP handler class
In the App_Code directory of the website, createHelloWorldHandler
.
Add the following code to the class file.
Visual Basic |
|
Imports System.Web Public Class HelloWorldHandler Implements IHttpHandler Public Sub ProcessRequest(ByVal context As _ System.Web.HttpContext) Implements _ System.Web.IHttpHandler.ProcessRequest Dim request As HttpRequest = context.Request Dim response As HttpResponse = context.Response ' This handler is called whenever a file ending ' in .sample is requested. A file with that extension ' does not need to exist. response.Write(" |
C # |
|
using System.Web; public class HelloWorldHandler : IHttpHandler { public HelloWorldHandler() { } public void ProcessRequest(HttpContext context) { HttpRequest Request = context.Request; HttpResponse Response = context.Response; // This handler is called whenever a file ending // in .sample is requested. A file with that extension // does not need to exist. Response.Write(" |
This code implements the ProcessRequest method and writes a string to the current Object Attribute.
Register a custom HTTP handler
After creating a custom HTTP handler class, you must register it in the Web. config file of the application. In this way, ASP. NET can find this handler when receiving requests for resources whose URLs end with. sample.
Register a custom HTTP handler in the Web. config file
Add a Web. config file to the website (if the file does not exist ).
Add the highlighted elements below to the Web. config file.
|
|
<configuration> <system.web> |
This code registers a Custom Handler by class name and maps the. sample file extension to the handler.
Configure the HTTP handler extension in IIS 6.0
IIS only transmits requests for certain file types to ASP. NET for processing. By default, files with. aspx,. ascx,. asmx, and other file extensions have been mapped to ASP. net isapi extensions (Aspnet_isapi.dll) in IIS 6.0 ). However, if you want ASP. NET to process custom URL extensions, you must map these extensions in IIS. For more information, see ASP. NET lifecycle.
Map the. sample file extension to ASP. NET in IIS 6.0
Open Internet Information Service (IIS) manager ".
Right-click the application name and click Properties ".
Note: |
For instructions on creating an ASP. NET application, see . |
Click the "virtual directory" tab, and then click "Configure ".
On the ing tab, click Add ".
The "Add/edit application extension ing" dialog box is displayed.
In the executable file box, type or browse to the Aspnet_isapi.dll file. By default, the file is located in the following locations.
|
|
%windows%\Microsoft.NET\Framework\version\ |
Note: |
You can obtain the complete path and file name from other mappings (such as. aspx file ing. |
In the extension box, type. sample.
Clear the check box "check whether a file exists.
Click OK and then close the IIS manager.
Test a custom HTTP handler
After creating and registering a custom HTTP handler, you can test it by requesting resources with the. sample file extension.
Test a custom HTTP handler
In the browser, enter a URL pointing to the Web application and ending with. sample, as shown below:
|
|
http://localhost/SampleApplication/test.sample |
Will be displayed inHelloWorldHandler
Class.