What does HttpContext do?

Source: Internet
Author: User
Tags httpcontext

This is a description of MSDN for HttpContext: 
HttpContext class: Encapsulates all HTTP-specific information about an individual HTTP request. (Is the context information, what is called context?). Personal feeling is not clear. 
Here's what MSDN notes about it:
For inheritance IHttpModule And IHttpHandler Interface provides a class for the current HTTP request.HttpContext The reference to the object. The object provides an internal Request, Response And Server Access to the property.
You can click this link to view MSDN HttpContext class. Includes instructions and how to use them.

So, Internet crazy search (network is good AH.)
This is the HttpContext Class I added:
1.Life cycle: Clicking from a client user and generating a request to the server starts---the server finishes processing the request and the build returns to the client.
Note: For each different user's request, the server will create a new HttpContext instance until the request is finished and the server destroys the instance.
2.Why would there be a HttpContext class?: In the ASP era, everyone was using request,respose,server and so on in the Code of the. asp page, and so on. But in the era of ASP, this approach has been unable to satisfy the application, ( For example, when we are dealing with the request in IHttpModule, what method do we use to obtain it? So the HttpContext class was created, and Request,respose,server and so on were encapsulated, and ensure that the entire request period can be called anytime, anywhere.
3.particularity: Of course, HttpContext is more than just this feature. ASP. It also provides a number of special features. For example, the cache. and Httpcontext.item, through which you can store some temporary data in advance of the HttpContext life cycle, Easy to use at any time. Specifically, you can refer to the MSDN section above for more information.

Usage
1. Pass data between two forms
Look at the following code:

For WebForm1:

private void Page_Load (object sender, System.EventArgs e)
{
ArrayList list = new ArrayList (4);
List. ADD ("this list");
List. ADD ("is for");
List. ADD ("WebForm2");
List. ADD ("to see.");

context.items["webform1list"] = list;

Server.Transfer ("webform2.aspx");
}

For WebForm2:

private void Page_Load (object sender, System.EventArgs e)
{
ArrayList list = context.items["Webform1list"] as ArrayList;

if (list! = null)
{
foreach (string s in list)
{
Response.Write (s);
}

}

In the WebForm1 page loading process, through the Server.Transfer method, the state of WebForm1 is passed to the Webform2,webform2 can get it, and make some corresponding processing. Usage 1 can also be done with the session.

2. Intercept HTTP requests and make special processing
It is mainly used in the Application_BeginRequest method. The Application_BeginRequest method is defined in the Global.asax.cs. You can intercept the HTTP request when it's just started, and do some special processing. You cannot do this here because the session object has not been created yet.

With the Context.item collection, you can have the entire lifetime of the HTTP request through httpmodules, httphandlers, Webforms, and application events

Source: http://www.cnblogs.com/ajunForNet/archive/2013/01/22/2872214.html

What does HttpContext do?

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.