why do we use HttpContext.Current?

HttpContext is an object that wraps all http related information into one place. HttpContext.Current is a context that has been created during the active request. Here is the list of some data that you can obtain from it.

  1. Request type (Post, Get)
  2. Request parameters (querystring, posted data)
  3. User's IP address
  4. Cookies

Further you can control your output through this object. In Items property, which is a dictionary, you can store instances of objects to ensure that they are created once for the request. You can control the output stream applying your custom filters.

This is a short list of that what you can do with this property.


It's a way to get access to the current HttpContext someplace that may not have a reference to the context but is within an active web request.


That's like saying "Why do I need to go to a bank to get money?", to which the answer is "Because that's where the money is.

To answer your question. Because that's where the Session is. It's really that simple. You don't have to know why, just that that's where it is.

There's a much longer explanation, which other people are giving with all the technical details. But in the end, the answer just boils down to this.

Tags:

C#

Asp.Net