Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.Web.HttpContext.Current does not exist

Tags:

c#

asp.net

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?

like image 859
onedevteam.com Avatar asked Sep 25 '15 20:09

onedevteam.com


People also ask

How can get HttpContext current in ASP.NET Core?

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.

What is HttpContext system?

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.

Where is HttpContext stored?

It is stored in the memory of the server and the value is available for the entire lifetime of the request.


2 Answers

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.

like image 103
Franchesco Avatar answered Sep 18 '22 06:09

Franchesco


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())
    );
like image 21
Arghya C Avatar answered Sep 18 '22 06:09

Arghya C