Asp. NET (Httpmodule,httphandler)

Source: Internet
Author: User
Tags config http request httpcontext iis

  at a previous ASP, when requesting a *.asp paging file, the HTTP request was first intercepted by a process called Inetinfo.exe, which is actually the WWW service. Once intercepted, it will transfer the request to the Asp.dll process, which interprets the ASP page and then returns the interpreted data stream to the client browser. In fact, ASP.DLL is an ISAPI file attached to IIS, which is responsible for the interpretation of files such as ASP files, ASA,

ASP. NET HTTP request processing method
When the client requests a *.aspx paging file to the Web server, it is similar to the ASP. This HTTP request is also intercepted by the Inetinfo.exe process (WWW service), which determines the file suffix and then transfers the request to ASPNET_ISAPI. DLL and ASPNET_ISAPI. The DLL sends the HTTP request to aspnet_wp via an HTTP pipeline pipeline. EXE process, when this HTTP request enters the aspnet_wp. EXE process, the ASP.NET framework processes the HTTP request through httpruntime, and returns the result to the client after processing is completed.
------------------------------------
When an HTTP request is sent to httpruntime, the HTTP request continues to be sent to a call to a HttpApplication A container of factory, which gives a HttpApplication instance to handle incoming HTTP requests, which in turn go into the following containers:
HttpModule--> HttpHandler Factory--> HttpHandler
When the HttpHandler method inside the system is processed, the entire HTTP request is processed. The client also gets the corresponding Dongdong.
The process flow of the full HTTP request in the ASP.net framework:
Httprequest-->inetinfo.exe->aspnet_isapi. Dll-->http pipeline-->aspnet_wp. Exe-->httpruntime-->httpapplication Factory-->httpapplication-->httpmodule-->httphandler Factory-->httphandler-->httphandler.processrequest ()
If you want to intercept a httprequest in midstream and do some of your own processing, This should be done inside the HttpRuntime runtime, and in HttpModule this container.
----------------------------------------
-------------------------------------
The HttpModule of the system itself implements a IHttpModule interface, and of course our own class can implement the IHttpModule interface, which replaces the HttpModule object of the system.
ASP. NET system, the default HttpModule:

Defaultauthenticationmodule Ensure that authentication objects exist in the context. This class cannot be inherited.
FileAuthorizationModule Verify that the remote user has NT permissions to access the requested file. This class cannot be inherited.
FormsAuthenticationModule enables the ASP.net application to use Forms authentication. This class cannot be inherited.
PassportAuthenticationModule provides a wrapper around the Passportauthentication service. This class cannot be inherited.
SessionStateModule provides session-state services for applications.
UrlAuthorizationModule provides a URL-based authorization service to allow or deny access to a specified resource. This class cannot be inherited.
WindowsAuthenticationModule enables the ASP.net application to use Windows/iis authentication. Cannot inherit this class

--------------------------------------
The default HttpModule of these systems are configured in file Machine.config, and the web.config that we use when we develop it is when the ASP.net framework initiates processing an HTTP request, It loads the Web.config file in the directory of the Machine.config and the request page in turn, and if you have a httpmodule in machine, you can still remove the mapping from the Web.config file on the page.
public class Helloworldmodule:ihttpmodule
{
Public HelloWorldModule ()
{
}

Public String ModuleName
{
get {return "HelloWorldModule";}
}

In the Init function, register for HttpApplication
Events by adding your handlers.
public void Init (HttpApplication application)
{
Application. BeginRequest =
(this.) New EventHandler (this. Application_BeginRequest));
Application. endrequest =
(this.) New EventHandler (this. Application_EndRequest));
}

private void Application_BeginRequest (Object source,
EventArgs e)
{
Create HttpApplication and HttpContext objects to access
Request and Response properties.
HttpApplication application = (HttpApplication) source;
HttpContext context = Application. context;
Context. Response.Write ("}

private void Application_EndRequest (Object source, EventArgs E)
{
HttpApplication application = (HttpApplication) source;
HttpContext context = Application. context;
Context. Response.Write ("}

public void Dispose ()
{
}
}

<system.web>
<add name= "HelloWorldModule" type= "HelloWorldModule"/>
</system.web>
--------------------------------------------------------------- --------------------
Deep HttpModule
An HTTP request is processed in turn to HttpModule and HttpHandler after being captured by the ASP.net framework. HM and HH is not completely independent, in fact, the HTTP request in the process of HM transmission will be in an event within the control to the HH, and the real processing in the HttpHandler after the completion of the implementation, HttpHandler will again control to return to HttpModule The parameters in the
HttpModule init () in the code above are HttpApplication types, which have many events, including Beginrequest,endrequest,authentiacterequest, and so on.
-----------------------------------------------------------------
IHttpHandler
It is asp.net The framework provides an interface that defines some of the system conventions that must be implemented if the processing of an HTTP request is to be implemented. In other words, if you want to handle certain types of HTTP request flows yourself, you need to implement these system conventions to do so. For example, a *.aspx file is used to handle this type of HTTP request, and the ASP.net framework will be processed by a HttpHandler class named System.Web.UI.PageHandlerFactory.
HH and HM, the system will initially start by asp.net The framework first loads the HttpHandler in Machine.config and then loads the user-defined HttpHandler class in the web.config in the directory where the Web application resides. But the relationship between the system and our custom HH is "overwrite", which means that ifWe have customized a HttpHandler class for "*.aspx", then the system will dispose of this HTTP request entirely to the HttpHandler class that we define. Our own HttpHandler class then needs to parse the HTTP request completely and make the processing. The most important method in the
IHttpHandler interface is ProcessRequest, which is httphandler used to process an HTTP request. When an HTTP request passes through the HttpModule container to the HttpHandler container, the framework invokes the HttpHandler ProcessRequest method to do the real processing of the HTTP request. Instead of locating HTTP requests for related pages directly over an internal default IHttpHandler container, the
framework is positioned on its internal default IHttpHandler factory. The function of IHttpHandler factory is to dispatch and manage the IHttpHandler containers which have been realized by many systems, the advantage of which is to greatly enhance the load of the system and enhance the efficiency.

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.