Can’t find Httpcontext.Current

Asked

Viewed 1,327 times

6

I’m trying to create a Urlhelper as follows:

UrlHelper urlHelper = new UrlHelper(HttpContext.Current.Request.RequestContext);

I already made the reference to System.Web, as I saw as solution in several places.

using System.Web;

The strange thing that is using is gray (Visual Studio 2015) which shows that he is not even using this reference.

The mistake is:

Error CS1061 'Httpcontextbase' does not contain a Definition for 'Current' and no Extension method 'Current' Accepting a first argument of type 'Httpcontextbase' could be found (are you Missing a using Directive or an Assembly Reference?)

  • you are using which mvc in vs2015? vnext? if it is vnext, you can forget about the system.web.

  • the project is mvc 5, Asp.net Aplication. vs2015, vnext? can not say...

  • Try Urlhelper urlHelper = new Urlhelper(System.Web.Httpcontext.Current.Request.Requestcontext);

  • vnext is the mvc6.

  • It worked! Weird, because it doesn’t work with using?

  • Using is right, the problem is that Httpcontext that you were pulling was from Controller and not SYSTEM.WEB

  • 1

    you can user the controller’s Httpcontext, just call Httpcontext.Request.Requestcontext (without Current), which works the same way.

Show 2 more comments

1 answer

6


Of Soen:

To have a reference to HttpContext.Current you need to change the term

HttpContext.Current

for

System.Web.HttpContext.Current

That’s because the class Controller defines a property with the name HttpContext, defined as

public HttpContextBase HttpContext { get; }

HttpContext in class Controller returns a HttpContextBase who doesn’t own the property Current.

That’s why you need to use the qualified namespace

Browser other questions tagged

You are not signed in. Login or sign up in order to post.