why do we use HttpContext.Current?

I don't know, why do we use HttpContext.Current? In this property I use it for Session but I don't know why!

public static string Name
         if (HttpContext.Current.Session["_n_"] != null)
            return HttpContext.Current.Session["_n_"].ToString();
         else return "";
         HttpContext.Current.Session["_n_"] = value;

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.

why do we use HttpContext.Current?, at the beginning of a request and destroyed when the request is completed. I didn't say that you should not use Session (actually I said it and I am saying it right now but not in the answer you linked to). I said that you should not use HttpContext.Current to access the current HTTP context. – Darin Dimitrov Apr 16 '12 at 21:08

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.

HttpContext Class in ASP.Net, only if you access it in a thread that handles incoming requests. That's why it works "when i use this code in another class of a page". I'm trying to access a Page within an HttpModule and I think I should do this by calling HttpContext.Current.Handler (This should reference the current page) but I'm getting null all the time. I'm developing using .Net 3.5 framework. I'm checking this on AuthorizeRequest and AuthenticateRequest. Thanks.

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.

HttpContext Object for Developers, A HttpContext object holds information about the current HTTP request. The important point is, whenever we make a new HTTP request or response then the Httpcontext object is created. Yes each time it is created it creates a server current state of a HTTP request and response. Clearly HttpContext.Current is not null only if you access it in a thread that handles incoming requests. That's why it works "when i use this code in another class of a page". It won't work in the scheduling related class because relevant code is not executed on a valid thread, but a background thread, which has no HTTP context associated with.

before asp.net MVC in a web form, there were classes request, response where you can get cookies and session and those staff in MVC all the HTTP information like request and response and their properties are now inside HTTpcontext.

Why is HttpContext.Current null?, But when we will implement session in service based application. (Let's think WebMethod in code behind which must be defined as static if we  HttpContext.Current also makes for more brittle code since it can be abused and misused. For example, you can use it in business layer since it is convenient but it will break if you choose to use an alternative presentation layer other than ASP.NET. Generally static methods are nowadays frowned upon since they cannot be dependency-injected.

System.Web.HttpContext vs System.Web.MVC.HttpContext, Current.Items and storing data in Session variable. In this blog post, I am going to describe what are the different scenarios where we can use  Protected Sub Page_Load(sender As Object, e As EventArgs) Dim context As HttpContext = HttpContext.Current Response.Write("<p>HttpContext.Current Example:</p>") ' Add three custom exceptions. context.AddError(New Exception("New Exception #1")) context.AddError(New Exception("New Exception #2")) context.AddError(New Exception("New Exception #3

HttpContext.Current Property (System.Web), Current; and WHY is this used in the code ? public static ModelCCMS CCMSInstance { get { HttpContext current = HttpContext.Cur Current we can grab the current Request, Response, Session, and. Application objects  Most of the time we access it within a Controller, but sometimes we need it outside of that. We're looking to write unit tests for the majority of our code. This means that we need to test code that calls HttpContext.Current. As soon as we do that, we get the dreaded NullReferenceException and our test fails. The way we get around that is to

Difference Between HttpContext.Current.Items and HttpContext , To answer this question In a single statement, you can use HttpContext.Current.​Items for very short term storage. By Short term storage means,  And here we are, we try to access HttpContext.Current, which is still null since we left MyLoginPage.cs This is why we get a NullReference Exception while trying to process a response. I want to precise that we are currently trying to adapt a wsp solution from SP2007/WinServer 2003/IIS6 to SP2010/WinServer 2008/IIS7.

  • See stackoverflow.com/a/5060948/224370
  • We used HttpContext.Current for accessing session object in class
  • thanks it was useful to me :)
  • It hasn't changed. Under ASP.NET MVC are the same classes in the System.Web namespace. They are called HttpRequest, HttpResponse, ... and you can access the instances of the current request through the property HttpContext.Current like before. WebForms has properties in the Page class for this purpose, but HttpContext exists as well.
  • I didn't say that it changed I said the request and response classes are encapsulated inside Httpcontext, but you 100% correct also ^^ as he can access the request directly from request property inside the controller