Process of processing requests by ASP. NET Server

Source: Internet
Author: User

Previously, debuglzq introduced the simplest Web server. Obviously, the web server just completed is just beginning in Taiyuan. If you want to create a more powerful ASP.. NET Server, Cassini is a good start point. Cassini is a simple ASP. NET Server released by Ms. It is also the predecessor of ASP. NET development server in Visual Studio. It does not require IIS support and uses Socket to process network connections. Cassini provides all the source code. The download link for the latest version of debuglzq found in codeplex is as follows: http://cassinidev.codeplex.com /. Dmitryr provides Cassini maintenance information in his blog. The following is his blog address: Workshop.

Go to the topic...

Step 1:

Assume that the request has reached ASP. to process requests, Asp. net creates an httprequest object on the server to represent the request parameters, and an httpresponse object to represent the response processing object. In addition, there is a ing between the virtual path of the website for processing objects of the httpserverutility type and the path of the file system on the server.That is to say, in order to process an HTTP request, we need a large number of objects to represent the data required for processing the request. The request processing method needs to receive many parameters for processing, ASP.. Net defines the httpcontext type to uniformly handle Parameter Representation issues. 

For each request, ASP. NET will create an httpcontext object instance for processing this request. This object instance willIt is used to pass all required parameters in the processing process of the ASP. NET Server.After the request arrives at the ASP. NET Server, the object will be created. After a request is processed, the object will be discarded. Because this object is required in most processing processes, the httpcontext class also providesCurrentTo directly obtain the current context object without using the parameters of the method. Of course, if there is no request, the result obtained through this attribute will be null. For more information about common attributes and methods in httpcontext, see msdn: http://msdn.microsoft.com/zh-cn/library/system.web.httpcontext.aspx

Although there are not many opportunities to use the methods provided by httpcontext, httpcontext provides some underlying methods that are useful when compiling httpmodule, for example:The rewritepath method is used to replace the original request address in httprequest with the specified address, so that ASP. NET considers the actual request address as the new address in subsequent processing.This method is very useful in sessions without cookies. It is used to obtain the sessionid embedded in the URL and correct the URL address to a normal URL. In this way, subsequent processing will not be affected. In addition, this method is also useful in URL rewriting. It can map a seemingly static URL to a common ASP. NET processing.

Step 2:

After an httpcontext object is created, httpruntime creates an object for processing requests. The object type is httpapplication.

(Supplement: The system. Web. httpruntime class is the entry for processing the entire ASP. NET Server. There is a system. Web. httpruntime class in each web application domain. This class provides a series of static attributes, reflecting the setting information of the Web application domain .)

In ASP. net, httpruntime manages a definition in system. for an instance of the httpapplicationfactory class in the Web namespace, httpapplicationfactory manages the httpapplication object in factory mode and maintains an httpapplication Object pool in httpapplicationfactory so that the created httpapplication object can be reused. However, each httpapplication object is only used to process one request at a time. In this way, ASP. NET programmers do not need to consider the concurrent processing of multiple requests in httpapplication.

In actual request processing, we still need to do a lot of work, for example, checking which user initiated the current request, in this way, we can perform different processing for different users, or determine whether to process user requests based on the user, and return a response that lacks the corresponding permissions for users who do not have permissions.If we complete these tasks in one method, it will obviously lead to over-bloated methods.In httpapplicationEvent MechanismBy sending multiple events in sequence during the processing process, this processing process is divided into multiple steps. This processing mechanism is usually calledMPs queue. Next we will discuss in detail the internal mechanism of processing pipelines and multiple events of httpapplication.

·Httpapplication MPs queue

The so-called pipeline Processing refers to decomposing the processing process into multiple processing steps when dealing with complicated problems. We refer to this processing method after multiple steps as processing pipelines. In. net, with the powerful power of events, we can encapsulate complex processing steps through the processing pipeline, and expose multiple steps of the processing process to programmers through events, this allows programmers to expand management pipelines. This topic describes how httpapplication processes pipelines.

In a pipeline, it often exposes a large number of events and provides a programmer's extension mechanism through these events.

However, for a class with a large number of events, defining a large number of events means that the cost of creating an event is required when creating an object. in. net, the so-called event is a restricted delegate member. Defining multiple events means that more storage space is required for the created object. To solve this problem. componentmodel. the component class provides the basis for processing multiple events: the events attribute. Its type is system. componentmodel. eventhandlerlist is a linear dictionary. When an event is required, the event is saved to the set through the key,There is no corresponding event, so there will be no cost for creating the eventIn this way, multiple event objects can be managed in a collection through eventhandlerlist, saving the space occupied by event objects.

Httpapplication objects are important objects for processing requests in ASP. NET. However, this type of object instance is not created by programmers,Created by ASP. NET. To facilitate the expansion of processing, httpapplication uses the pipeline processing method. The processing process is divided into multiple steps, and each step is exposed to the program domain in the form of events, these events are triggered sequentially according to the fixed processing order. By writing event processing methods, programmers can customize the extended processing process of each request.

For httpapplication, to ASP. NET 4.0, 19 standard events are provided, and the order of events triggered is as follows:

See msdn: http://msdn.microsoft.com/zh-cn/library/system.web.httpapplication.aspx

·Brief Introduction to the processing process:

In ASP. NET, the ASP. NET Server processes the same request and passes through the processing pipeline of the httpapplication.. The internal processing process of the pipeline is fixed. In each stage of the server's request processing, the corresponding events are triggered along with the processing, this allows programmers to complete custom processing at various stages of processing.

The first event triggered is beginrequest, which marks the beginning of processing the ASP. NET Server and is also the first event that programmers can process in ASP. NET for requests.

After processing the request, the first important task isDetermine the identity of the requesting user to implement the security mechanismThis job provides the opportunity to check the identity of the currently requested user through the authenticaterequest and postauthenticaterequest events. Obviously, authenticaterequest indicates that the user's identity is checked, while postauthenticaterequest indicates that the user's identity has been checked. After the check, the user can obtain the identity through the user attribute of httpcontext. The attribute type is system. security. principal. iprincipal and iprincipal have another name named identity, whose type is system. security. principal. iidentity attribute. iidentity has a bool-type attribute isauthenticated, which indicates whether the user of the current request has been verified, or whether the user is an anonymous user. If isauthenticated is false, it indicates that this is an anonymous user. If it is true, the name attribute of the string type is used, which is the user name of the current request.

After ASP. NET obtains the identity of a user, it startsCheck request PermissionsWork. When 4th events are triggered by authorizerequest, it indicates that the user's permission check is started, and the 5th events postauthorizerequest indicate that the user's permission check has been completed. If the user does not pass the security check, the remaining events are skipped and the endrequest event is directly triggered to end the request processing process.

When the user obtains the request permission, the server starts to prepare the fastest way for the user to get the response result.The resolverequestcache event marks a check in the result of the previous cache to see if the results can be directly obtained from the results of the previous cache. postresolverequestcache indicates the completion of the cache check.

When the results cannot be obtained from the cache, the results of the current request must be calculated through one processing. In ASP. NET, the object used to process requests to get results is called the handler. in ASP. NET, many handlers are provided, and programmers can customize the handler. To process this request, Asp. NET must find a processing program that processes the current request according to the matching rules. The postmaprequesthandler event indicates ASP. net has obtained the handler, And the handler attribute of httpcontext indicates the handler object. From the above analysis, we can see that the handler attribute of httpcontext has practical significance here.

After the processing program is obtained, it cannot be processed immediately. This is because the Processing request still requires a lot of data related to the request. For example, when the user sends a request to the server, some user-specific data is saved on the server. Since the ASP era, session has emerged in web development and provides session-based status management. Due to the stateless HTTP protocol, status management is a core issue in Web development.

To obtain the exclusive data that the user previously saved, the acquirerequeststate event provides a starting point for the programmer. The postacquirerequeststate event indicates that the user data acquisition has been completed and can be used during processing.

The prerequesthandlerexecute event is used to notify programmers that the processing program is about to start processing. If the user's status has been obtained, there is still work that needs to be done before the processing, in this case, handle it.

After the prerequesthandlerexecute event, the ASP. NET Server processes the request by executing a processing program. This processing program may be a web form or a Web service. This task is completed between 11th events and 12th events.

After the processing program is completed, the server starts scanning and tail. The postrequesthandlerexecute event notifies the programmer that the processing program of the ASP. NET Server has been completed.

After processing is completed, because the user may modify the user-specific exclusive data in the processing program, the modified User status data may need to be serialized or saved. The releaserequeststate event notifies the programmer to release the State data. The postreleaserequeststate indicates that the release has been completed.

After the processing is complete, if you want to cache the processing result so that the result can be directly used in subsequent requests, the updaterequestcache event provides an opportunity to process it, postupdaterequestcache indicates that the cache has been updated.

In ASP. NET 4.0, two new events are added to log processing: logrequest indicates that the request is recorded in the log, and postlogrequest indicates that the log processing is completed.

In the preceding event,The request does not have to go through all eventsFor example, if the user does not pass the authorization check, the subsequent events will be skipped. However, the endrequest event is the event of the last httpapplication processing pipeline for all requests, ASP is also processed by programmers. net to process the last opportunity in the request. After this event, the processing result will be responded to the browser to complete processing of the ASP. NET Server.

[Click "Green Channel" belowFollow debuglzq", Debuglzq grows with you !]

 

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.