HTTP Handles in ASP.NET

xiaoxiao2021-03-06  27

1. Request for HTTP Handles ASP.NET is based on a Pipeline model, ASP.NET sends all HTTP requests to the HTTP component in this pipe. Each component performs some corresponding actions after receiving the HTTP request. When the HTTP request passes all HTTP Modules programs, it will be handled by an HTTP Handle program, and the processed result will return to HTTP Modules in the pipe. This  龉   校 饔 饔 膆 TTP Module can have multiple, however called HTTP Handle can only be one. The process is as follows:

It can be seen that each input HTTP request will eventually be processed by an HTTP Handle program. HTTP Handle is an instance of a class that implements the system.web.ihttphandler interface, some similar to ISAPI extensions. In HTTP Handles is implemented: ProcessRequest: This method is used to handle HTTP requests, which is the most core method of HTTP Handles isReusable: A property, returns a BOOL value to indicate whether this HTTP HANDLE can be reused to process multiple The same type of HTTP request.

Second, the class registered in the configuration file HTTP Handles HTTP Handles can be registered in the web.config or machine.config file. In this way, once there is a corresponding HTTP request input, this HTTP Handle class will be instantiated. In Web.config or Machine.Config files we use and nodes to add HTTP Handle classes for our app:

In 1, the VERB attribute illustrates the HTTP request method supported by the Handle, such as supporting the POST and GET mode, the VERB attribute is "post, get"; if all request methods are supported, the VERB attribute is used "* ". 2, the PATH attribute illustrates the handle of which files to process, for example, you just want to call this handle when request my.possible file, then the Path property is "my.possible", if you want all the suffix name Both the Possible file (* .possible) is handled by this handle, the Path property is "* .possible". 3, the Type property specifies the namespace, class name, and accessory name (project name) of the HANDLE class. ASP.NET Runtime will first go to the bin directory of the application to find the DLL of the accessory, if not found to the GAC in the GAC.

In fact, many of the features in ASP.NET themselves are also implemented using HTTP Handlers, and ASP.NET uses many Handle classes to handle .aspx, .smx, .soap, and some other ASP.NET files. You can find the following code in the machine.config file:

........................................

From the above configuration, you can see that the request for the .aspx file is handled by the System.Web.ui.PageHandlerFactory class, and the .config file is processed by the System.Web.httpForbiddenhandler class, you can guess this class will Returns a class file that cannot be requested. Third, the implementation of the HTTP Handles class Here we use C # to create a new Handle class to handle new file types, such as files that are .possible as the suffix name. 1. Let's create a web application in VS.NET, named myHandler, then add a class file newhandler.cs to establish a class that implements the IHTTPHANDLER interface:

Using system; using system.Web;

Namespace myHandler {///

/// summary description for newhandler. /// public class newhandler: httphandler {public newhandler () {// // Todo: add constructor logic here //}

#region Implementation of IHttpHandler public void ProcessRequest (System.Web.HttpContext context) {HttpResponse objResponse = context.Response; objResponse.Write ( "

Hi, This is a test ! "); Objresponse.write (" ");}

Public bool isreusable {get {return true;}} #endregion}}}}}} #ENDREGON}}}

In the implementation of the ProcessRequest method, we just get HTTPRESPONSE objects for HTTPCONTEXT and some HTMLs are sent like the client. Returns true in the ISREUSABLE implementation, indicating that the Handle class can handle a request for multiple pairs .possible files. Note: If you want to use Session in HTTP Handlers, you also need to implement the IREQUIRESSESSIONSTATE interface, and the IRequiresSessionState interface is just a flag, so you don't need to implement any specific methods, so you only need to change the classes to: public class newhandler: httphandler, IrequiressessionState can. 2, open the web.config file, register the newly created Handle class above:

3, add ISAPI extensions to add our new compact name .possible, the specific process is: IIS - "Select the" Default website "to right -" Select "Properties -" "Main Directory" - "Configuration" - "Add" button - "Add" button - "In the pop-up dialog box, click the" Browse "button, select the ASPNET_ISAPI.dll file, and fill in the Possible in the extension, as shown below : Finally click the OK button. This way we can enter http://localhost/myhandler/xxx.possible in the browser to call this handle. Of course, we just got a simple example here, so enter any * .possible is the same effect. We can first analyze the requested URL in NewHandler, and then make different corresponding corresponding to different URLs, such as jump to different real-existing ASPX pages, which is also the MVC mode in the web design mode in ASP.NET One of the core parts implemented by Front Controller, please see: http://msdn.microsoft.com/architecture/patterns/default.aspx? Pull = / library / en-us / dnpatterns / html / desfrontcontroller.asp.

转载请注明原文地址:https://www.9cbs.com/read-81534.html

New Post(0)