ASP. NET life cycle –asp.net Request life cycle (iv)

Source: Internet
Author: User

do not use special methods to handle request life cycle events

The HttpApplication class is the base class for global application classes and defines general C # events that can be used directly. The use of standard C # events or special methods is a matter of personal preference, and if you like, you can mix the two ways to use them.

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingsystem.web;5 usingSYSTEM.WEB.MVC;6 usingSystem.Web.Routing;7 8 namespaceSimpleapp9 {Ten      Public classMvcApplication:System.Web.HttpApplication One     { A          Publicmvcapplication () -         { -BeginRequest + = (src, args) = RecordEvent ("beginrequest"); theAuthenticateRequest + = (src, args) = RecordEvent ("AuthenticateRequest"); -Postauthenticaterequest + = (src, args) = RecordEvent ("postauthenticaterequest"); -         } -  +         protected voidApplication_Start () -         { + Arearegistration.registerallareas (); A routeconfig.registerroutes (routetable.routes); at         } -  -         Private voidRecordEvent (stringname) -         { -list<string> eventlist = application["Events"] aslist<string>; -             if(EventList = =NULL) in             { -application["Events"] = EventList =Newlist<string>(); to             } + Eventlist.add (name); -         } the     } *}
View Code

We have added a constructor to the Mvcapplication class and established an event handler for three of the request lifecycle events. And these three events I used a lambda expression to invoke the RecordEvent method, storing the name of the event for the controller to read, as in the previous example.

Tip: There are no standard C # events to replace the Application_Start and Application_End methods. We are only able to receive these two notifications in a special way.

Handling multiple events using a single method

If we want to use a method to handle multiple life cycle events without relying on lambda expressions, then the two attributes defined in the System.Web.HttpContext class can help us. The details of the current request and application status are provided in the HttpContext class, which we'll cover in more detail later. Currently, however, we will only describe the two attributes that are associated with handling life cycle events.

Name Describe
Currentnotification This property uses the value in the System.Web.RequestNotification enumeration to indicate the current app event.
Ispostnotification This property returns True if the currently applied event name returned by the Currentnotification property is a variant of post<name>.

These two properties are a bit odd because both need to figure out which event is being processed. The Currentnotification property returns a Requestnotification enumeration value that defines a subset of the HttpApplication events. The Ispostnotification property value is calculated based on whether the currently triggered event is an event like AcquireRequestState or a postacquirerequeststate event that is paired with it. In the HttpApplication class, you can get to the HttpContext object through the Context property.

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingsystem.web;5 usingSYSTEM.WEB.MVC;6 usingSystem.Web.Routing;7 8 namespaceSimpleapp9 {Ten      Public classMvcApplication:System.Web.HttpApplication One     { A          Publicmvcapplication () -         { -BeginRequest + =RecordEvent; theAuthenticateRequest + =RecordEvent; -Postauthenticaterequest + =RecordEvent; -         } -  +         protected voidApplication_Start () -         { + Arearegistration.registerallareas (); A routeconfig.registerroutes (routetable.routes); at         } -  -         Private voidRecordEvent (Objectsrc, EventArgs args) -         { -list<string> eventlist = application["Events"] aslist<string>; -             if(EventList = =NULL) in             { -application["Events"] = EventList =Newlist<string>(); to             } +             stringName =Context.CurrentNotification.ToString (); -             if(context.ispostnotification) the             { *Name ="Post"+name; $             }Panax Notoginseng Eventlist.add (name); -         } the     } +}
View Code

I changed the signature of the RecordEvent method and therefore adopted the standard event handler signature: An object represents the source of the event, and the EventArgs object describes the event. Instead of using the values provided by these two parameters, I used the information provided by the Context.currentnotification property and the Context.ispostnotification property.

I do not understand why Microsoft implements events in such a way, but if you do not want to use special methods or lambda expressions, you must use this approach. Note that in the above code, I used the ToString method on the Context.currentnotification property, which is necessary because the Currentnotification property returns a The System.Web.RequestNotification enumeration value. See the table below:

Table 1–requestnotification Enumeration values

Value Describe
BeginRequest Corresponds to the BeginRequest event
AuthenticateRequest Corresponds to AuthenticateRequest and Postauthenticaterequest events
AuthorizeRequest Corresponds to the AuthorizeRequest event
Resolverequestcache Corresponds to Resolverequestcache and Postresolverequestcache events
Maprequesthandler Corresponds to Maprequesthandler and Postmaprequesthandler events
AcquireRequestState Corresponds to AcquireRequestState and Postrequeststate events
Preexecuterequesthandler Corresponds to the Preexecuterequesthandler event
Executerequesthandler Corresponds to the Executerequesthandler event
ReleaseRequestState Corresponds to ReleaseRequestState and Postreleaserequeststate events
Updaterequestcache Corresponds to the Updaterequestcahce event
Logrequest Corresponds to the Logrequest event
EndRequest Corresponds to the EndRequest event
Sendresponse Indicates that the response is being sent--not exactly corresponding to the presendrequestheaders and Presendrequestcontent events

[According to Adam Freeman–pro ASP 5 Platform]

ASP. NET life cycle –asp.net Request life cycle (iv)

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.