What does the word HttpContext mean in .ashx? Let's talk about it to you
HttpContext class: Encapsulates all HTTP-specific information about individual HTTP requests.
In each stage of the processing request execution chain, there is an object that passes between each object, that is, the context information of the request is stored, which is the HttpContext object. The HttpContext encapsulates all the information ASP.NET a single request to be processed. When the request handling mechanism is established, the HttpContext class is instantiated with an HttpRuntime object, which then goes through various stages of the request lifetime
Introduction to HttpContext: Maintain data for a single user, a single request, and the data is only maintained for the duration of that request. is provided to maintain the values that need to be passed between different HttpModules and HttpHandlers. It can also be used to maintain the appropriate information for a complete request.
The Current property is a useful static member that returns the currently requested HttpContex object. Items are hash tables that share data between the modules and handlers involved in processing requests. Each custom module or handler is able to add its own information to the requested HttpContext object, and the information stored in the Items is eventually used by the page, but this information can only be accessed during the execution of the request HttpContext Definition: Encapsulates all HTTP-specific information about individual HTTP requests.
In fact, the Http request and all the information returned are in it.
HttpContext comes from System.Runtime.Remoting.Messaging.CallContext.HostContext. This HostContext definition is to get or set the host context associated with the current thread (so the HttpContext can only be used in the thread of the current request. )
HttpContext is actually stored in the CallContext.HostContext propertyIf you're still curious about HostContext, you can see it yourself with Reflector.exe, I don't want to post any more code, because some types and methods are not public.
HttpContext.Current.Items can be used when sharing data between HttpModule and HTTPHandler, because every user request goes through the HTTP runtime pipeline HttpModule, HTTPHandler. When you implement the IHttpMoudle method to pass information to the user request via HttpMoudle. You can use HttpContext.Current.Items to transfer data in different request pages and different HttpModules, but once the request ends and the data is posted, the data in this collection will be lost by itself.
For example, every time we make a request, we want to use the same DbContext context object in ef, and the code is as follows:
|