onedevteam.com
onedevteam.com

Reputation: 4178

System.Web.HttpContext.Current does not exist

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?

Upvotes: 15

Views: 27347

Answers (2)

Franchesco
Franchesco

Reputation: 85

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.

Upvotes: 3

Arghya C
Arghya C

Reputation: 10068

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())
    );

Upvotes: 12

Related Questions