ASP. NET life cycle –asp.net Context object (v)

Source: Internet
Author: User

ASP. NET Context Object

ASP. NET provides a series of objects used to give the current request, the response that will be returned to the client, and the Web app itself to provide contextual information. Indirectly, these context objects can also be used to go back to the core ASP.

The context object provides information about the application, the current request, and the response associated with the current request. Access to most important ASP. NET platform services, such as security and status data, is also provided. We can use context objects in the controllers and views of the MVC framework to adjust the response of our application based on the current request or application state data. We also use these objects when creating modules or processors. Based on an ASP. NET service (such as mobile device detection), the MVC framework uses context objects to handle requests.

Understanding ASP. NET Context Objects

The core class of the context is System.Web.HttpContext. It is visible throughout the ASP. NET Framework and the MVC framework, and it acts as a gateway to other context objects and ASP. NET features and services. In fact, in the ASP. NET Framework, the HttpContext class is very central, and many of its important properties are discussed in the next step.

Table 1-most commonly used HttpContext members

Name Describe
Application Returns a HttpApplicationState object that is used to manage application state data.
ApplicationInstance Returns the HttpApplication object associated with the current request.
Cache Returns a cache object for caching data.
Current (static) Returns the HttpContext object for the current request.
Currenthanlder Returns a Ihttphanlder instance used to generate content for the current request.
Isdebuggingenabled If the debugger is attached to an ASP. NET application, it returns TRUE. We can use this to perform debugging-related activities, but if we do, remember to test complete without a debugger before deployment.
Items Returns a collection that can be used to pass state data before participating in the component being processed by the current request.
GetSection (name) Gets the configuration block specified in the Web. config file.
Request Returns a HttpRequest object used to provide details of the request that is currently being processed.
Response Returns the details of a HttpResponse object used to provide the response that is currently being built and is about to be sent to the browser.
Session Returns a HttpSession object used to provide access to session state. This property will only return null until the Postacquirerequeststate application event is triggered.
Server Returns a HttpServerUtility object that contains the utility function, the most practical of which is the execution of the request processor.
Timestamp Returns a DateTime object that contains the time when the HttpContext object was created.
Trace Used to record diagnostic information.

HttpContext also defines methods and properties that can be used to manage the request lifecycle-such as currentnotification and Ispostnotification attributes. Other contextual object features, including those defined by HttpContext, are described next.

We can get an instance of the HttpContext class through the Context property in the global application class. The name of this property is not global. In the controller class or in a view, we need to use the HttpContext property to get it. If these methods fail, then we can use static httpcontext.current to get the HttpContext object associated with the current request.

Table 2-Obtaining a HttpContext instance in a different ASP.NET/MVC component

Component Technology
Controller Using the HttpContext attribute defined in the Controller, it is the base class for controllers in the MVC framework.
View Use the Context property defined in Webviewpage, which is the base class used to compile the Razor view.
Global Application Class Use one of the convenient property--context defined in HttpApplication.
Module When called, a HttpContext object is passed in the Init method, and the life cycle event handler is passed to a HttpApplication object, which defines a Context property.
Handler A HttpContext object is passed when the Proceerequest method is called.
Universally We can always get the HttpContext object associated with the current request through the HttpContext.Current property.

Tip: Each request creates a series of new context objects, and when we get to the HttpRequest or HttpResponse object, we get a global application class instance associated with the current request. Or, in other words, we don't have to worry about locating a context object for a specified request.

Notice that I did not include the app component in the table above. You can get the HttpContext object for the current request by using the static HttpContext.Current property, but I don't recommend it because it blurs the principle of separation of concerns before the model and controller. If a model requires information about a request, then we can pass it as a method parameter to the model by getting the information from the context object in the controller. This ensures that the model does not intervene in the controllers business, which also allows the model to be unit tested without the need for any ASP or MVC framework references.

Context, Base, and Wrapper classes

The properties listed in the previous table do not all return the same type. The properties in non-MVC framework components (global application classes, processors, and modules) return a HttpContext object, which is basically what we expected.

These contextual objects that precede the birth of the MVC framework make it difficult to unit test the code because they are highly coupled, requiring us to create a complete set of context objects each time we test it.

The properties of the components defined in the MVC framework,--controller and view, can be used to get the context object and return instances of different classes that inherit from the context class, which provides easy unit testing. The HttpContext property in the Controller class returns an instance of the HttpContextBase class, where all the context objects are represented by a class with a Base suffix (httprequestcontext, Httpresponsecontext, etc.), they are easier to instantiate, configure, and perform unit testing.

We sometimes need to create a Base object from an ASP. For example, we have an object, but we need to invoke a method that takes a Httpresponsebase object as a parameter. The ASP. NET class Library includes classes with the suffix Wrapper: httpcontextwrapper,httprequestwrapper, and so on. These classes inherit from the base class, and in MVC represent the ASP. NET Context class in an MVC-friendly Base class (so Httpcontextwrapper is inherited from HttpContextBase, which receives a HttpContext instance as constructor parameters). The Wrapper class constructor above is used as the constructor parameter, and the Httpcontextwrapper property is passed to the HttpContext instance it contains.

We cannot disassemble a Base object--such as converting Httprequestbase to HttpRequest. But we can always get the context object we need through static httpcontext.current, it will return a HttpContext object. In the controller we need to use the full name (System.Web.HttpContext.Current) for this property, because a HttpContext property is defined in it, but the HttpContextBase object is returned. In fact, I consider the context class of the ASP. NET Framework to be equivalent to the MVC Base class.

[According to Adam Freeman–pro ASP 5 Platform]

ASP. NET life cycle –asp.net Context object (v)

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.