Asp. NET in the HTTP Handles

Source: Internet
Author: User
Tags bool config http request httpcontext iis
asp.net one, about HTTP handles
Asp. NET's request process is based on a pipeline (pipeline) model, ASP. NET sends all HTTP requests (Requests) to the HTTP component (modules) in this pipeline. Each component takes some appropriate action after receiving an HTTP request. When the HTTP request passes through all HTTP modules programs, it will be processed by an HTTP handle program, and the processed results will be returned via the pipeline HTTP modules. In this whole process, the HTTP module that is invoked can have multiple, but the HTTP handle called is only one. The process is as follows:



You can see that each incoming HTTP request is eventually processed by an HTTP handle program. HTTP handle is an instance of a class that implements the System.Web.IHttpHandler interface, some similar to an ISAPI extension. Implemented in the HTTP handles are:
ProcessRequest: This method is used to process HTTP requests, which is the core method of HTTP handles
IsReusable: A property that returns a bool value to indicate whether an instance of this HTTP handle can be reused to handle multiple HTTP requests of the same type.

Registering the HTTP handles in the configuration file
The HTTP handles classes can be registered in Web.config or Machine.config files. Thus, once the corresponding HTTP request input is available, the HTTP handle class is instantiated. In Web.config or Machine.config files we use the <add verb= "Supported HTTP Verbs" path= "path" type= "Namespace.ClassName, AssemblyName"/>

In <add>
1. The Verb property describes the HTTP request methods supported by the handle, such as support for POST and get methods, verb property for "post,get", and "*" for verb properties if all requests are supported.
2. The Path property describes which file requests are invoked to handle the handle, for example, if you only want to invoke the handle when requesting my.possible file, the path attribute is "my.possible" If you want all files with a suffix named possible ( *.possible) is handled by the handle, the Path property is "*.possible."
3, the type attribute specifies the namespace, class name, and accessory name (project name) of the handle class. asp.net runtime will first go to the application's Bin directory to find the DLL for the accessory, and if not, find it in the GAC.

In fact, many of ASP.net's own functions are also implemented using HTTP handlers, ASP. NET uses a number of handle classes to handle. aspx,. asmx,. Soap, and some other asp.net files. You can find the following code in the Machine.config file:
<add verb= "*" path= "Trace.axd" type= "System.Web.Handlers.TraceHandler"/>

<add verb= "*" path= "*.aspx" type= "System.Web.UI.PageHandlerFactory"/>

<add verb= "*" path= "*.ashx" type= "System.Web.UI.SimpleHandlerFactory"/>

<add verb= "*" path= "*.config" type= "System.Web.HttpForbiddenHandler"/>

<add verb= "Get,head" path= "*" type= "System.Web.StaticFileHandler"/>

. . . . . .
. . . . . .
As you can see from the configuration above, the request for the. aspx file is handled by the System.Web.UI.PageHandlerFactory class, and as the. config file is handled by the System.Web.HttpForbiddenHandler class, you can guess that this class will Returns an error that the class file cannot be requested.

Implementation of HTTP handles class
Here we use C # to create a new handle class to handle new file types, such as files with the. possible suffix name.
1, we first create a Web application project in Vs.net, named MyHandler, and then add a class file NewHandler.cs to create a class that implements the IHttpHandler interface:

Using System;
Using System.Web;

Namespace MyHandler
{
<summary>
Summary description for Newhandler.
</summary>
public class Newhandler:ihttphandler
{
Public Newhandler ()
{
//
Todo:add constructor Logic here
//
}

#region implementation of IHttpHandler
public void ProcessRequest (System.Web.HttpContext context)
{
HttpResponse Objresponse = context. Response;
Objresponse.write ("Objresponse.write ("</center></body>}

public bool IsReusable
{
Get
{
return true;
}
}
#endregion
}
}
In the implementation of the ProcessRequest method, we simply obtained the HttpContext HttpResponse object and sent some HTML as the client. Returns true in the IsReusable implementation, which means that an instance of the handle class can handle multiple requests for the. possible file.

Note: If you want to use the session in HTTP handlers, you also need to implement the IRequiresSessionState interface, and the IRequiresSessionState interface is just a flag and does not need to implement any specific methods. So just change the class declaration to: public class newhandler:ihttphandler,irequiressessionstate.

2, open the Web.config file, register the newly created handle class:
<add verb= "*" path= "*.possible" type= "Myhandler.newhandler,myhandler"/>

3, add the ISAPI extension in IIS, add our new suffix name. Possible, the process is as follows:
iis--Select the "Default Web site" point right-"properties"-"Home Directory"-"Configuration"-"Add" button in the point "map"--"Click the Browse" button in the pop-up dialog box to select Aspnet_ Isapi.dll the file and fill in the possible in the extension, as shown in the following figure:



Finally click the OK button.

This allows us to enter http://localhost/MyHandler/xxx.possible in the browser to invoke the handle. Of course we're just holding a simple example here, so entering any *.possible is the same effect. We can analyze the URL of the request in the Newhandler ProcessRequest method, and then make a different corresponding according to different URLs, such as jump to different real aspx pages, This is also the web design pattern in the MVC pattern in the asp.net through the front controller implementation of one of the core, specifically see: http://msdn.microsoft.com/architecture/patterns/ Default.aspx?pull=/library/en-us/dnpatterns/html/desfrontcontroller.asp.



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.