Gain an in-depth understanding of ASP. NET operations

Source: Internet
Author: User

To do things, you must know the root cause, the principle, and the writing.ProgramWe should be more familiar with the program running mechanism. This article mainly introduces the operation insider of Asp.net.
Httpapplication triggers an event to notify your program of an event, so as to take charge of the Request flow. as httpapplication. part of the init () function occurs (use reflector to view the system. web. httpapplication. initinternal () method and httpapplication. resumesteps () method for more details), sets and starts a series of events consecutively, including executing all the processors (handler ). these event processors map to global. events automatically generated in asax are also mapped to all additional httpmodules (they are essentially additional event receivers (sinks) released by httpapplication )).

Both httpmodule and httphandler are based on the Web. the corresponding configuration in config is dynamically loaded and appended to the event processing chain. httpmodule is actually an event processor attached to a special httpapplication event. However, httphandler is the end point used to process "application-level request processing.

Both httpmodule and httphandler are in httpapplication. part of the init () function call is loaded and attached to the call chain. figure 6 shows different events, when they occur, and which part of the MPs queue they affect.

Figure 6-event transfer process in the ASP. net http pipeline. event-driven requests of the httpapplication object are transferred in the pipeline. The HTTP module can intercept these events and overwrite or extend existing functions.

Httpcontext, httpmodules, and httphandlers

Httpapplication itself knows nothing about the data sent to the application-it is just a message object that communicates through events. it triggers an event and transmits a message to the called function through the httpcontext object. the actual status data of the current request is maintained by the httpcontext object mentioned above. it provides all the request-specific data and keeps following the request from the beginning to the end of the pipeline. figure 7 shows ASP.. Net pipeline. note that the context object (httpcontext) is always your "friend" object from the beginning to the end of the request, you can save the information in an event processing function and retrieve it in a later event processing function.

Once the pipeline is started, httpapplication starts to trigger events one by one as shown in figure 6. each event processor is triggered. If an event is mounted, these processors execute their own tasks. the main task of this processing is to ultimately call the httphandler that receives the specific request. the processor (handler) is ASP. the core processing mechanism of net requests, usually at the application levelCodeExecuted. remember ASP. the net page and Web Service Framework are both implemented as httphandler, which is also the core of request processing. A module tends to be a preprocessing or post-processor that is passed to the context of the processor (handler. ASP. typical default processors in. Net include pre-processing authentication, caching, and various encoding Mechanisms in post-processing.

There is a lot of available information about httphandler and httpmodule, so to keep thisArticleAt a reasonable length, I will provide a brief introduction to the processor.

Httpmodule

 

When a request is transmitted in the pipeline, a series of events in the httpapplicaion object are triggered. we have seen these events in global. asax is released as an event. this method is specific to the application and may not always be what you want. if you want to create a generic httpapplication event hook that can be inserted into any web application, you can use httpmodule, Which is reusable and does not require specific language application code, only web. an entry in config.

The module is essentially a filter (fliter)-function is similar to the ISAPI filter, but it works in ASP. NET Request level. the module allows you to mount events for each request that uses the httpapplication object. these modules are used as class storage in external assembly ., in the web. the config file is configured and loaded when the application starts. by implementing specific interfaces and methods, the module is mounted to the httpapplication event chain. multiple httpmodules can be attached to the same event. The order of event processing depends on the order of the events on the web. the Order declared in config. below is the Web. the processor definition in config.

<Configuration>

<System. Web>

<Httpmodules>

<Add name = "basicauthmodule"

Type = "httphandlers. basicauth, WebStore"/>

</Httpmodules>

</System. Web>

</Configuration>

Note that you need to specify the complete type name and the Set name without the dll extension.

The module allows you to view each received Web request and execute an action based on the triggered event. the module is excellent in modifying request and response data. It can be used to provide custom authentication for specific programs or for ASP.. net. many ASP. NET functions, such as authentication and session engines are implemented as httpmodule.

Although httpmodule looks like an ISAPI filter, they all check that each of them passes through ASP. NET application requests, but they only check the ing to a single specific ASP.. NET application or virtual directory requests, that is, only requests mapped to ASP can be checked. NET Request. in this way, you can check all aspx pages or any other ing to ASP. net extension. you cannot check standard. htm or image files unless you explicitly map these extensions to ASP. net isapi dll, as shown in figure 1. A common application of this type may be to use modules to filter jpg image content in a specific directory and draw the 'sample' at the top layer using GDI +.

Implementing an HTTP module is very simple: you must implement the ihttpmodule interface that contains two functions (Init () and dispose. the passed event parameters include references to the httpapplication object, which gives you the ability to access the httpcontext object. you can mount the HTTP application event to these methods. for example, if you want to mount the authenticaterequest event to a module, you only need to do it as shown in List 5.

List 5: The basic HTTP module is very easy to implement

Public class basicauthcustommodule: ihttpmodule

{

Public void Init (httpapplication Application)

{

// *** Hook up any httpapplication events

Application. authenticaterequest + =

New eventhandler (this. onauthenticaterequest );

}

Public void dispose (){}

Public void onauthenticaterequest (Object source, eventargs)

{

Httpapplication APP = (httpapplication) source;

Httpcontext context = httpcontext. Current;

... Do what you have to do... }

}

Remember that your module has accessed the httpcontext object, from which you can access other ASP. net pipeline inherent objects, such as requests and responses, so that you can also receive user input information. but remember that some things may not be accessible. They can only be accessed after the processing chain.

You can mount multiple events in the init () method, so that you can implement multiple different functions in one module. however, it may be clearer to divide different logics into separate classes to modularize modules) in many cases, the function you implement may require you to mount multiple events. For example, a log filter may record the request start time in the beginrequest event, then, the end of the request is written to the log in the endrequest event.

Note: response. end () or httpapplication. completerequest () will "Copy near track" in the event chain of httpapplication and module ". see "Pay attention to response. end () to obtain more information.

Note response. End ()

when an httpmodule is created or. when you implement event hooks in asax, when you call response. end or appplication. pay special attention to completerequest. both functions end the current request and stop triggering subsequent events in the HTTP pipeline. Then, the control is returned to the web server. when you perform operations such as logging or content operations after the processing chain, they may be fooled because they are not triggered. for example, the logging example in the sample will fail, because if you call response. end (), the endrequest event will not be triggered.

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.