I'm trying to get some info about my users in asp.net mvc application with visual studio 2015.
When i try to to get info from request with
System.Web.HttpContext.Current req = new System.Web.HttpContext.Current();
I get an error Error CS0426 The type name 'Current' does not exist in the type 'HttpContext'
Anyone know how to fix this?
In ASP.NET Core, if we need to access the HttpContext in service, we can do so with the help of IHttpContextAccessor interface and its default implementation of HttpContextAccessor. It's only necessary to add this dependency if we want to access HttpContext in service.
The HttpContext encapsulates all the HTTP-specific information about a single HTTP request. When an HTTP request arrives at the server, the server processes the request and builds an HttpContext object. This object represents the request which your application code can use to create the response.
It is stored in the memory of the server and the value is available for the entire lifetime of the request.
Just add the System.Web
reference to your project and it'll be ok.
using System.Web;
does not generate an error even if the reference does not exist.
If you want to get the current context
System.Web.HttpContext currentContext = System.Web.HttpContext.Current;
If you want to create one (for some reason, like test)
System.Web.HttpContext newContext = new System.Web.HttpContext(
new System.Web.HttpRequest("", "http://example.com", ""),
new System.Web.HttpResponse(new System.IO.StringWriter())
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With