Synchronous http handler (IHttpHandler)

Source: Internet
Author: User

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
  1. In the App_Code directory of the website, createHelloWorldHandler.

  2. 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
  1. Add a Web. config file to the website (if the file does not exist ).

  2. 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
  1. Open Internet Information Service (IIS) manager ".

  2. Right-click the application name and click Properties ".

    Note:

    For instructions on creating an ASP. NET application, see .

  3. Click the "virtual directory" tab, and then click "Configure ".

  4. On the ing tab, click Add ".

    The "Add/edit application extension ing" dialog box is displayed.

  5. 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.

  6. In the extension box, type. sample.

  7. Clear the check box "check whether a file exists.

  8. 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 inHelloWorldHandlerClass.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.