Httphandler for custom HTTP processing and Applications in ASP. NET [go to 1]

Source: Internet
Author: User
Tags microsoft iis
When developing Microsoft IIS-based applications, developers can not only write ASP programs, but also use visual
C ++ and other development tools to develop ISAPI applications for more powerful functions. You can write two types of ISAPI extensions: ISAPI server Extention and ISAPI
However, writing ISAPI extended applications usually requires developers to develop and deploy applications.
When developing an ASP. NET application, we can still write an ISAPI application to expand the functions of IIS. However, ASP. NET provides us with another option-using HTTP handler
And HTTP module. This is achieved through the ihttphandler and ihttpmodule interfaces. HTTP handler provides APIs similar to ISAPI
Server extention, while httpmodule implements functions similar to ISAPI filter, and is much simpler than ISAPI in development and deployment.
The application httphandler and httpmodule enable the application to communicate with IIS
Interaction between low-level requests and response services of web servers. This article first introduces the concepts and basic usage of httphandler and httpmodule, and introduces a case of applying httpmodule to implement a permission system.

Basic Model of HTTP processing pipeline
To study httpmodule and ihttphandler, you must first have an understanding of ASP. NET processing pipelines.
In ASP. in the. NET application, the system uses a set of related classes to process client requests (requests), Asp. NET application processing mode can be called HTTP processing pipeline. Httpmodule and ihttphandler are two processes in the processing pipeline.
Classes in the HTTP processing pipeline are defined in the system. Web namespace, mainly including the following types: · httpworkerrequest
Abstract Classes define the basic methods for processing requests on ASP. Net pages. · HttpRuntime provides a set of services for processing applications. · HttpContext
Saves all the relevant context information for processing a request. · HttpApplicationFactory provides the application of the relevant directory; · HttpApplication
Defines common methods, attributes, and events for all ASP. Net applications. This class is also the base class of the application defined in the user's global. asax file; · Modules
Handle events before and after the request; · HandlerFactories provides Handlers in the application; · Handlers processes requests and responses.
The HTTP processing pipeline model is as follows:
 
Figure 1: HTTP
MPs queue
On Windows, HTTP
Pipline requires IIS support. To run an ASP. NET application, IIS requires the following two files: ASPNET_ISAPI.DLL and ASPNET_WP.EXE ·
ASPNET_ISAPI.DLL is an ISAPI Extention. It will be sent to IIS. Please transfer it to ASPNET_WP.EXE for processing ·
ASPNET_WP.EXE uses HttpRuntime to process the request as follows:
 
Figure 2: HTTP MPs queue on IIS
HttpHandler implementation
HttpHandler implements something similar to ISAPI
Extention, which processes the Request information and sends a Response ). The HttpHandler function is implemented through the IHttpHandler interface. In fact, we are writing ASP. net page, ASP. the base class inherited by the. Net page -- System. web. UI. page -- also implements the HttpHandler interface, which is also an HttpHandler. Let's take a look at its definition (C #):

Public class Page: TemplateControl, IhttpHandler

The IHttpHandler interface is defined as follows:
interface IHttpHandler
{
void
ProcessRequest(HttpContext ctx);
bool IsReuseable { get; }
}

In the API, ProcessRequest adds your own code and processes it accordingly. The IsReuseable attribute specifies whether the HttpHandler implementation class needs to be cached.
The following example shows the basic usage of HttpHandler: 1. Create a project named MyNameSpace and add a class named MyHandler. The Code is as follows:

Example 1:
namespace MyNameSpace
{
public class MyHandler :
IHttpHandler
{
public void ProcessRequest(HttpContext ctx)
{

HttpResponse Response
Response.Write("This is my handler");}
public
bool IsReusable
{
get { return true; }
}
}
}


2. Compile the above Code to generate the MyNameSpace. Dll file;
3. Create a New WebApplication project, open a WebApplication project, add the file MyNameSpace. Dll to the project reference, or copy it to the bin directory of the project;
4. modify Web. Config and add the following content:
<configuration>
<system.web>

<httpHndlers>
<add verb="*" path="*.aspx"
type="
MyNameSpace.MyHandr, MyNameSpace" />
</httpHndlers>

</system.web>
</configuration>


Options in the configuration file :·
Verb can be "GET" or "POST", indicating that the GET or POST request is processed. "*" Indicates that all requests are processed. ·
Path indicates processing the corresponding file. "*. aspx" indicates processing the requests sent to all ASPX pages. You can specify a path, such as "/test/*. aspx", indicating that the ASPX file under the test directory is processed only.
· In the Type attribute, the string before the comma specifies the Class Name of the HttpHandler implementation class, and the subsequent string specifies the name of the Dll file.
Currently, on any ASPX page in the request project, only one line of text is displayed:
This is my handler

Because our Custom Handler intercepts all requests sent to the ASPX page and uses its own method to process these requests.
To make our ASPX page run smoothly, we need to modify the Web. Config file:
<configuration>

<system.web>
<httpHndlers>
<add verb="*" path="*.foo"

type=" MyNameSpace.MyHandr,hander" />
</httpHndlers>

</system.web>
</configuration>

In order to make requests for files with the suffix ". foo" be intercepted and run by our Handler, we need some additional work. Open the IIS console, click the site, and select "properties". The site Properties dialog box is displayed. Select the Home Directory option. 3:

 

Figure 3: Web site Properties dialog box

Select Configuration. The application configuration dialog box is displayed. Add ". foo" to application ing. 4:
 
Figure 4: Add application ing

Now we can add a. foo file to the project. When sending a request to this file, the browser displays:
This is my handler

Access to other ASPX files is not affected.
Implement Handler Factory

Another option to implement the HttpHandler function is to implement a Handler Factory, which is implemented through the IHttpHandlerFactory interface.
The IHttpHandlerFactory interface is defined as follows:
interface IHttpHandlerFactory
{

IHttpHandler GetHandler(HttpContext ctx,
string requestType,
string
url,
string pathTranslated);
void ReleaseHandler(IHttpHandler handler);

}

The GetHandler method is called at the beginning of the request, while the ReleaseHandler method is called at the end of the request and when no Handler is needed.
The process of using HttpHandlerFactory is generally as follows:
First, define the actual HttpHandler class. This class will be called in HandlerFactory for actual processing:
public class
BasicHandler : IHttpHandler { ... }

Then, define your own handlerfactory:
public class
BasicHandlerFactory : IHttpHandlerFactory
{
public IHttpHandler
GetHandler(HttpContext ctx,
string requestType,
string url,
string
pathTranslated)
{
return new BasicHandler();
}
public void
ReleaseHandler(IHttpHandler handler) {}
}

Finally, register the factory in the web. config file:

<configuration>
<system.web>
<httpHandlers>
<add
verb="POST" path="*.foo"
type="MyNamespace.BasicHandlerFactory, MyAssembly"
/>
</httpHandlers>
</system.web>
</configuration>

Asynchronous Handler

Ihttpasynchandler can implement asynchronous processing of HTTP requests. The ihttpasynchandler interface inherits ihttphandler and must implement processrequest.
Method and isreusable attribute. At the same time, you must implement beginprocessrequest and endprocessrequest
Method. Beginprocessrequest starts an asynchronous call to process a single HTTP request, while endprocessrequest executes the cleaning code at the end of the process.
The implementation and registration of ihttpasynchandler are similar to those of ihttphandler. You can refer to the relevant documentation of msdn. Now, do you want
What are the concepts and applications of handler? In the next article, we will mainly introduce the application of HTTP module and provide an example of using httpmodule to implement the permission system.

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.