Knowledge of IHttpHandler and IHttpModule

Source: Internet
Author: User
Tags httpcontext implement

In the last article we talked about the Httpapplicatiion pipeline incident, so how do I deal with these pipe events, and how the request is performed in ASP.net, let's take a look at IHttpHandler and IHttpModule

Introduction:

Handling Httpapplicatiion Events

HttpApplication provides an event-based extension mechanism that allows programmers to extend process processing with the aid of handling events in pipelines. Since the HttpApplication object is created and maintained by the ASP.net infrastructure, how can you obtain this object reference to facilitate the registration of event handling for HttpApplication objects, providing two ways to To solve this problem: IHttpModule and global.aspx, the core of these two ways is ihttpmodule a few, below we mainly speak IHttpModule.

Body:

Use and usage of IHttpModule

In ASP.net, the IHttpModule interface, defined under the System.Web namespace, specializes in event handling that defines HttpApplication objects.

The class that implements the IHttpModule interface becomes HttpModule. The IHttpModule interface is defined as follows, consisting of only two members:

Public interface IHttpModule
{
    void Dispose ()
    void Init (HttpApplication context)
}

Where the Dispose method is used to recycle the unmanaged resources used by the module, and if not, return directly.

Most importantly, the second method, Init, can see that this method takes a HttpApplication type of argument, In ASP.net, whenever you create a HttpApplication object instance, you iterate through the registered HttpModule type, creating an instance object of the registered HttpModule type by reflection, The HttpApplication instance is passed to each HttpModule through the Init method, so that the HttpModule object can complete the event registration for the HttpApplication object at the first time.

For example, if you want to write a httpmodule for a postauthenticaterequest event, you can complete the following registration

Pulic class Xxx:ihttpmodule
{
    void Dispose ()
    void Init (HttpApplication app)
    {
        app. Postauthencaterequest+=new EventHandler (app_postauthencaterequest)
    }
}

Of course, implementing the IHttpModule interface is only part of implementing the HttpModule, In the asp.net used in the HttpModule also to be registered in the site configuration file to really effective, and in the ASP.net use, which we are not in the explanation, the following look at the IHttpHandler.

IHttpHandler

In asp.net, the real processing of a request is between the handler, the Prerequesthandlerexcute and Postrequesthandlerexcute of the HttpApplication19 standard event, Prerequesthand Lerexcute is responsible for the gay programmer, the handler is about to begin work, Postrequesthandlerexcute event comrade programmer asp.net Server handler has been completed. So what is the role of HttpApplication? We can think of it as a request-arrival handler and a pipeline to leave the handler, which provides a mechanism for uniformly handling all requests, allowing us to preprocess and process the work before and after the request is actually processed.

The handler is responsible for the actual request processing, and for the site developer, most of the development work is done around the handler. (In fact, we can see that our page class is page implementation of the IHttpHandler interface) in fact, access to HttpApplication event processing is not much, the process in different Web development technology has different names, In ASP.net, for HttpHandler.

In asp.net, all handler classes must implement the IHttpHandler interface or implement the IHttpAsyncHandler interface, we can clearly see the difference, one is the synchronization interface, one is the interface of the asynchronous processing mode. Then we usually use the interface of the synchronous mode. Here's a brief introduction

Both of these interfaces are defined under System.Web, and the IHttpHandler interface is defined as follows

Public interface IHttpHandler
{
    void ProcessRequest (HttpContext context)
    bool Isrequest{get;}

ProcessRequest is the primary method of this interface, receiving a HttpContext type of request context object through which the handler can obtain the information needed to process the request, through which the response attribute of the parameter can be used to manage the object of the response. You can return the processing results of the server to the client.

The Isrequest property indicates whether the handler object can also be cached after it is used, in the future request processing.

Of course, we also need to register the handler, each processing program is used to handle a class of requests, the handler and the matching relationship between the request can be configured in the Web site configuration parameters to set.

This column more highlights: http://www.bianceng.cnhttp://www.bianceng.cn/webkf/aspx/

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.