ASP. NET: HttpModule, HttpHandler

Source: Internet
Author: User

Asp.net Architecture

Processing of asp.net requests
-------------------
What must be mastered by HttpModule
What HttpHandler must master is very useful
The above two instances
---------------------
Asp.net event model Mechanism

-----------------------
I
The customer's request page is processed by the dynamic Connection Library aspnet_isapi.dll, which sends the requested aspx file to CLR for compilation and execution, and then returns the Html stream to the browser.
--------------------------
2. Page events
Execution sequence
Page_Init: initialization value or connection
Page_Load: mainly uses IsPostBack. This event mainly performs a series of operations to create an asp.net page or response for the first time.
Client events caused by shipping. The page and control view status have been restored before this event.
Page_DataBind: it can be called at the page level or in a single control.
DataBind_PreRender: Pre-rendering of data binding, which is triggered just before the view status and Rendering Control are saved.
Page_Unload: This event executes the final cleaning.
Uncertain event
Page_Error: if an unhandled exception occurs during page processing, an error event is triggered.
Page_AbortTransaction: Transaction event. If a transaction has been terminated during transaction processing, this event is triggered and is commonly used in shopping carts.
Page_CommitTransaction: this event is triggered if the transaction is successful.
-------------------
Events in Global. asax (execution sequence)
Application_Start: triggered when the application starts
Application_BeginRquest: triggered when an http request starts.
Application_AuthenticateRequest: triggered when the application approves an http request
Session_Start: triggered when the session is started
Application_EndRequest: triggered when the Htttp request ends
Session_End: triggered when the session ends
Application_End: triggered when the application ends
Application_Error: triggered when an error occurs.
----------------------
ISAPI: inserts some components into the web server to expand the functions and enhance the functions of the web server.
ISAPI: extended, win32 dynamic link library, such as aspnet_isapi.dll, can regard ISAPI extension as a common application, which processes HTTP requests.
ISAPI: filter. The web server sends the request to the related filter. Next, the filter may modify the request and perform some operations.
Processing of ASP. NET requests:
Based on the pipeline model, ASP. NET transmits http requests to all modules in the pipeline. Each module receives HTTP requests and has full control. Once the request passes through all the HTTP modules, it is finally processed by the HTTP processing program. The HTTP handler processes the request and the result passes through the HTTP module in the module pipeline again.
-----------
Httpmodule
ISAPI filter: IIS itself does not support dynamic pages, that is, it only supports static HTML page content,. asp. aspx. cgi. php, etc. IIS does not know that if it processes these suffix tags, it will treat it as text and send it to the client without any processing. To solve this problem, IIS has a mechanism called ISAPI filter. It is a COM component.
When the ASP. NET service registers to IIS, it registers the file extensions that each extension can process to IIS (for example, *. ascx *. aspx ). After the extension is started, it processes the files that cannot be processed by IIS according to the defined method, and then redirects the control to the process that specifically processes the Code. In asp.net, It is aspnet_isapi.dll. Let this process start to process the code, generate standard HTML code, add the code to the original HTML, and finally return the complete HTML to IIS, IIS then sends the content to the client.
----------------
HttpModule
The Http module implements the ISAPI filter function, which is a. net component that implements the System. Web. IHttpModule interface .. These components register themselves in some events and insert themselves into the ASP. NET Request Processing pipeline. When these events occur, ASP. NET calls the HTTP module that is interested in the request, so that the module can process the request. Sometimes you need to worry over the http request. Note that it does not overwrite other http modules that come with the system. The configuration is complete in Machine. config.
--------------------------------------
HttpHandler
It implements the ISAPI Extention function, which processes the Request information and sends the Response ). The IHttpHandler interface must be implemented through the HttpHandler function. The HTTP handler is A. NET component that implements the System. Web. IHttpHandler interface. Any class that implements this interface can be used to process input Http requests. It is an Http processing program.

In the past ASP, when a *. ASP page file is requested, this httprequest will first be intercepted by an inetinfo.exe process, which is actually a www Service. After interception, the request is forwarded 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. It is responsible for interpreting and executing files such as ASP files and ASA,
-------------------------------------
ASP. net http Request Handling Method
When the client requests a * www process interception (www Service) from the web server and determines the file suffix, it forwards the request to ASPNET_ISAPI.DLL, and ASPNET_ISAPI.DLL uses an Http PipeLine, send the http request to the ASPNET_WP.EXE process. After the HTTP request enters the ASPNET_WP.EXE process, the asp.net framework processes the Http request through HttpRuntime and then returns the result to the client.
------------------------------------
After an http request is sent to HttpRuntime, the request will be sent to a container called HttpApplication Factory, the container will provide an HttpApplication instance to process the passed http requests, and then the Http requests will enter the following containers in sequence:
HttpModule-> HttpHandler Factory-> HttpHandler
After the HttpHandler's ProcessRequest method in the system is processed, the entire Http Request is processed and the client obtains the corresponding information.
Complete http request processing process in asp.net framework:
HttpRequest-> inetinfo.exe-> response-> Http Pipeline-> ASPNET_WP.EXE-> HttpRuntime-> HttpApplication Factory-> HttpApplication-> HttpModule-> HttpHandler Factory-> HttpHandler. processRequest ()
If you want to intercept an httpRequest in the middle and perform some processing on your own, you should implement this internally during the HttpRuntime runtime, specifically in the HttpModule container.
----------------------------------------
-------------------------------------
The system's HttpModule implements an IHttpModule interface. Of course, our own class can also implement the IHttpModule interface, which can replace the system's HttpModule object.
The default HttpModule in ASP. NET:

DefaultAuthenticationModule ensures that the Authentication object exists in the context. This class cannot be inherited.
FileAuthorizationModule verifies that the remote user has the NT permission to access the requested file. This class cannot be inherited.
FormsAuthenticationModule enables ASP. NET Applications to use Forms authentication. This class cannot be inherited.
The PassportAuthenticationModule provides packaging for the PassportAuthentication service. This class cannot be inherited.
SessionStateModule provides session status services for applications.
UrlAuthorizationModule provides URL-based authorization services to allow or deny access to specified resources. This class cannot be inherited.
WindowsAuthenticationModule enables ASP. NET Applications to use Windows/IIS authentication. Unable to inherit this class

--------------------------------------
The default HttpModule of these systems is in the file machine. the configuration in config and the web. the config relationship is in ASP. when the net framework starts to process an Http Request, it will load the machine in sequence. the web. config file. If an HttpModule is configured in the machine, you can still. remove the ing from the config file.
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 + =
(New EventHandler (this. Application_BeginRequest ));
Application. EndRequest + =
(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>
& L

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.