IHttpHandler interface implements http Processing

Source: Internet
Author: User

Do you want to learn HTTP processing program programming? Well, the first step is to familiarize yourself with the IHttpHandler interface. The HTTP handler is only a hosting class that implements this interface. More specifically, the IHttpHandler interface is implemented for Synchronous HTTP processing programs, while the IHttpAsyncHandler interface is implemented for asynchronous HTTP processing programs. Let's first look at the synchronization processing program.

The IHttpHandler interface contract defines the actions that the HTTP handler needs to take to process an HTTP request synchronously.

1. IHttpHandler interface member

The IHttpHandler API defines only two members: ProcessRequest and IsReusable, as shown in table 2.1. ProcessRequest is a method, while IsReusable is a Boolean logical attribute.

Table 2.1 IHttpHandler interface member

Members

Description

IsReusable

This attribute returns a Boolean value indicating whether another request can use the HTTP handler instance.

ProcessRequest

This method processes HTTP requests

The IsReusable attribute of the Page class returns false, indicating that a new instance of the HTTP request is required to serve a Page request. In general, we make it return false in all circumstances, and require it to perform some meaningful Processing Based on Different request loads. The handler that is used as a simple barrier for filtering special requests can set IsReusable to true to save some CPU cycles. I will use a specific instance to illustrate this point later.

The ProcessRequest method has the following signature:

Void ProcessRequest (HttpContext context );

It uses the request context as the input and ensures that the request receives the service. When ProcessRequest returns a synchronous processing program, you are prepared to send the output to the client.

2. A simple HTTP processing program

Once again, the HTTP handler is only a class that implements the IHttpHandler interface. The request output is created in the ProcessRequest method, as shown in the following code:

Using System. Web;

Namespace proaspnetappsadvanced. CS. Components

{

Public class SimpleHandler: IHttpHandler

{

// Override the ProcessRequest method

Public void ProcessRequest (HttpContext context)

{

Context. Response. Write ("<H1> Hello, I'm an HTTP handler </H1> ");

}

// Override the IsReusable property

Public bool IsReusable

{

Get {return true ;}

}

}

}

We need an entry point that can call the processing program. In this context, the entry point of the handler code is only an HTTP endpoint-that is, a public URL. This URL must have a unique name so that the IIS and ASP. NET runtime libraries can map it to this code. During registration, the ing between the HTTP handler and Web server resources is established through the web. config file.

<Configuration>

<System. web>

<HttpHandlers>

<Add verb = "*" path = "myHandler. aspx"

Type = "proaspnetadvanced advanced. CS. Components. SimpleHandler"/>

</HttpHandlers>

</System. web>

</Configuration>

The

Note: If you enter the preceding settings in the machine. config file, the SimpleHandler component will be registered as a callable component in all Web applications hosted on the server machine.

If you call myHandler. aspx URL, the result 2.2 is displayed.

Figure 2.2 example of an HTTP handler responding to the myHandler. aspx request

The method described here is the fastest and easiest way to use HTTP handlers. However, we still need to know more about HTTP handler registration and many other options to use. Now, let's consider a more complex example of HTTP processing program.

HTTP handler and ASP. NET page

We should use HTTP processing program resources to implement the unique functions of the application. They need to be processed faster than conventional Web pages. In any case, the HTTP handler returns a valid HTTP response with the content type and body. A. ashx request or any other request managed by a Custom Handler may result in faster code than a. aspx resource. ASP. NET is usually faster to process a Custom Handler, because it does not have to trigger any intermediate events (such as Init and Load) on user code, and does not have to host any view status, nor does it support any sending-back mechanism. Generally, a request to a custom HTTP handler is similar to a request to A. aspx resource, where only the rendering step occurs. In addition, it may take longer to find the correct HTTP handler required to serve a page request because it involves an intermediate object in a page handler factory.

That is to say, we should remember that the ASP. NET page is just an HTTP processing program-although it is a very complicated and advanced HTTP processing program. The underlying processing mechanisms are identical. If custom HTTP handlers that meet certain requirements are generally faster than pages, they are generally implemented to directly obtain the given results. For example, suppose we need to display an image from a database -- this is a topic we will discuss in detail in Chapter 9th, we still need to bind an Image control to a URL of the appropriate MIME type of the service. Should this URL be a page? Of course, it can be a page. However, if you use a custom HTTP processing program, the process is usually faster.

ASP. NET pages are complex objects that are provided by a custom and complex HTTP handler (Page class. In terms of service custom resources, you must use a suitable processing program, only package

 

Including the required intelligence and complexity. To illustrate this, if we want to run a query and provide the bytes of an image stored in a database, we do not need to manage any view status or sending back, you do not need to send events to the application.

Should I use a general. ashx resource or a custom extension? This depends on the functions to be implemented. The ASHX method is designed for relatively simple scenarios. There is almost no need to pass any parameters (or no parameters at all), and you can use query strings to introduce them. If a custom document is to be processed and has information that is organized in a non-flat or complex layout, it is best to use custom extensions.

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.