Asp. NET HTTP modules and handlers

Source: Internet
Author: User
Tags config http request

Introduced

At the beginning of the internet age, the needs of clients are very limited;. htm files can meet their needs. However, over time, the expansion of client requirements extends beyond the functionality contained in. htm files or static files.

Developers need to expand or extend the functionality of the Web server. Web server vendors have designed different solutions, but all follow the same theme, "Inserting certain components into a Web server." All Web server supplemental technologies allow developers to build and insert components to enhance the functionality of the Web server. Microsoft has proposed the ISAPI (Internet Server API), Netscape has proposed NSAPI (Netscape Server API) and so on.

ISAPI is an important technology that allows us to enhance the ability of an ISAPI-compliant Web server (IIS is a Web server that is compatible with ISAPI). We use the following components to achieve this goal:

· ISAPI Extension

· ISAPI filters

ISAPI extensions are implemented using the WIN32 dynamic link library. You can think of an ISAPI extension as an ordinary application. The process target of the ISAPI extension is the HTTP request. This means you have to call them to activate them. You can think of an ISAPI filter as just a filter. Each time the client makes a request to the server, the request passes through the filter. The client does not need to specify a filter in the request, simply send the request to the Web server, and the Web server passes the request to the relevant filter. The filter may then modify the request, perform some login operations, and so on.

Because of the complexity of these components, it is difficult to implement them. Developers have had to use C/COM + + to develop these components, but for many people, using C + + to develop is simply synonymous with pain.

So what does asp.net provide to achieve these functions? Asp. NET provides HttpHandler (HTTP handlers) and HttpModule (HTTP modules).

Before delving into the details of these components, it is valuable to understand how HTTP requests are processed through HTTP modules and HTTP handlers.

Building the sample application

I set up some of the following C # projects to demonstrate different components of the application:

· Newhandler (HTTP handler)

· Webapp (Demo HTTP handler)

· Securitymodules (HTTP Module)

· WEBAPP2 (Demo HTTP module)

Installation steps for these applications:

· Unlock the code in the attached zip file.

· Create two virtual directories WebApp and WEBAPP2; point the two directories to the actual physical directory of the WebApp and WEBAPP2 applications.

· Copy the Newhandler.dll file from the Newhandler project to the WebApp application's Bin directory.

· Copy the SecurityModules.dll files from the Securitymodules project to the bin directory of the WEBAPP2 application.

Asp. NET request process

Asp. NET request processing is based on the piping model, in which asp.net the HTTP request is passed to all modules in the pipeline. Each module receives HTTP requests and has Full control permissions. The module can handle the request in any way that it thinks fit. Once the request passes through all HTTP modules, it is eventually processed by the HTTP handler. The HTTP handler processes the request, and the result passes through the HTTP module in the pipeline again:

Note that during the processing of HTTP requests, only one HTTP handler can be invoked, but multiple HTTP modules may be invoked.

HTTP handlers

An HTTP handler is a. NET component that implements the System.Web.IHttpHandler interface. Any class that implements the IHttpHandler interface can be used to process incoming HTTP requests. HTTP handlers are somewhat similar to ISAPI extensions. The difference between HTTP handlers and ISAPI extensions is that they can be called directly in the URL using the file name of the HTTP handler, similar to the ISAPI extension.

The HTTP handler implements the following methods:

Method name Describe
ProcessRequest This method is actually the core of the HTTP handler. We call this method to handle HTTP requests.
IsReusable We call this property to determine whether an instance of an HTTP handler can be used to handle the same other type of request. HTTP handlers can return TRUE or false to indicate whether they can be reused.

You can use Web.config or Machine.config files to map these classes to HTTP requests. When the mapping is complete, asp.net instantiates the HTTP handler when the corresponding request is received. We will explain how to define all of these details in the Web.config and/or machine.config files.

Asp. NET also supports extensions of HTTP handlers through the IHttpHandlerFactory interface. Asp. NET provides the ability to route HTTP requests to objects that implement the IHttpHandlerFactory interface's classes. In addition, ASP. NET also utilizes the factory design pattern. This pattern provides an interface for building a set of related objects without providing a specific class of functionality. Simply put, you can think of a class that is used to establish an HTTP handler object that relies on passing in parameters as a factory (factory). We do not specify the specific HTTP handlers that need to be instantiated; The HTTP handler factory handles this transaction. The advantage of this is that if the implementation of the object that implements the IHttpHandler interface changes in the future, the client will not be affected as long as the interface remains the same.

The following is a list of methods in the IHttpHandlerFactory interface:

Method name Describe
GetHandler This method is responsible for setting up the appropriate handler and returning its pointer to the calling code (asp.net runtime). The handler object returned by this method should implement the IHttpHandler interface.
Releasehandler This method is responsible for releasing the HTTP handler after the request processing completes. The Factory implementation determines its operation. The Factory implementation can be an actual instance of destroying, or it can be put into a buffer pool for later use.

Registering HTTP handlers and HTTP handler factories in a configuration file

Related Article

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.