. NET Learning Notes---HttpContext request context

Source: Internet
Author: User

I. Overview of HttpContext

HttpContext the HttpApplication-based processing pipeline, because the HttpContext object runs through the entire process , the state data can be passed from the front end of the HttpApplication processing pipeline to the back end of the pipeline. The delivery task for the completed state.

  The HttpContext life cycle starts from the HTTP request received by the server to the end of the reaction sent back to the client.

In WebForm or class libraries (including MVC) projects, the HttpContext object can be obtained through the current static property.

HttpContext context = HttpContext.Current;

If it is in the controller of ASP. NET MVC, pass this. HttpContext, you can get the HttpContextBase object.

HttpContextBase context = this. HttpContext;

If you can get this in the MVC view:

@Html. Viewcontext.httpcontext

  In MVC, HttpContextBase is the HttpContext in WebForm.

In addition, there are some places where some people may be confused, because there seems to be a lot of HttpContext in the page, such as Request,response,cache,session and so on. What is the relationship between them? Here we use the session, request to explain.

Create a new WebForm program and enter the following code:

        protected void Page_Load (object sender, EventArgs e)        {            HttpContext context = HttpContext.Current;            Context. Session.add ("1", "Liu Bei");            Response.Write (page.session["1"]);      Output Liu Bei            Response.Write (object. ReferenceEquals (context. Session, Page.session));  Output True to indicate that this is the same object            Response.Write (object. ReferenceEquals (context. Request, Page.Request));          }

The output is as follows:

  

Needless to say, HttpContext is the same object as the object in page. HttpContext is also available only for easy access to these request objects in class library projects.

Second, HttpContext Common properties

HttpContext Common Properties:

name Description
Application Gets the HttpApplicationState object for the current HTTP request.
Cache Gets the Cache object for the current application domain.
Current Gets or sets the HttpContext object for the current HTTP request.
Currenthandler Gets the IHttpHandler object that represents the handler that is currently executing.
Handler Gets or sets the IHttpHandler object that is responsible for processing HTTP requests.
Items Gets a collection of key/values that can be used to organize and share data between the IHttpModule interface and the IHttpHandler interface during an HTTP request.
Previoushandler Gets the parent handler for the IHttpHandler object.
Profile Gets the ProfileBase object for the current user profile.
Request Gets the HttpRequest object for the current HTTP request.
Response Gets the HttpResponse object for the current HTTP response.
Server Gets the HttpServerUtility object that provides the method that is used to process the WEB request.
Session Gets the HttpSessionState object for the current HTTP request.
Skipauthorization Gets or sets a value that specifies whether the UrlAuthorizationModule object should skip authorization checks on the current request.
Timestamp Gets the point in time of the current HTTP request processing request

For this class, you can't write a demo. Because all of them are return objects, the key to delving into ASP is to get a deeper understanding of the objects returned inside.

        protected void Page_Load (object sender, EventArgs e)        {            HttpContext context = HttpContext.Current;            Context. Items.Add ("KK", "through the context of the Save parameter!") ");        }

The current property of HttpContext is important, and this property allows you to find it anywhere.

If you want to provide some static properties and only want to associate with a single request, it is recommended that you implement the instance properties of Httpcontext.items.

Iii. Common methods of HttpContext

HttpContext Common methods:

name Description
Adderror Adds an exception to the exception collection for the current HTTP request.
ClearError Clears all errors for the current HTTP request.
GetGlobalResourceObject is overloaded. Gets the application-level resources.
GetLocalResourceObject is overloaded. Gets the page-level resource.
GetSection Gets the specified configuration section for the default configuration of the current application.
Remaphandler Used to specify a handler for the request.
RewritePath Rewrite the path so that subsequent ASP. NET thinks this is the real address. The RewritePath is used in a non-Cookie session state.

Example:

        Public ActionResult Index ()        {            HttpContextBase context = this. HttpContext;            Context. RewritePath ("1.html");      Rewrite the URL method so that subsequent ASP. This is the actual request address            Response.Write (context. Request.Url.AbsolutePath);            return View ();        }

Output:

  

. NET Learning Notes---HttpContext request context

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.