C# – Razor Views not seeing System.Web.Mvc.HtmlHelper

asp.net-mvc-4crazor

I am in the process of upgrading to MVC4. I have followed the instructions at http://www.asp.net/whitepapers/mvc4-release-notes#_Toc303253806 but in my Razor views and layouts I have errors like

  • 'System.Web.WebPages.Html.HtmlHelper' does not contain a definition
    for 'BeginRouteForm' and no extension method 'BeginRouteForm'
    accepting a first argument of type
    'System.Web.WebPages.Html.HtmlHelper' could be found (are you missing
    a using directive or an assembly reference?)

I also have errors like:

  • "The name 'Viewbag' does not exist in the current context

When I hover over @Html I can see it is of type System.Web.WebPages.Html.HtmlHelper not System.Web.Mvc.HtmlHelper

Not really sure what other information is pertinent but this is where I am:

  • I haven't updated class librarys etc.. in the sln to .net 4.5 yet.
  • The project was created in VS2010, but I am doing the migration in
    VS2012
  • The project opens and runs OK with VS2012, .Net 4.0, MVC 3

Any pointers appreciated.

Edit:
All references and files in web.config upgraded to:

  • System.Web.Mvc, Version=4.0.0.0
  • System.Web.WebPages, Version=2.0.0.0
  • System.Web.Helpers, Version=2.0.0.0
  • System.Web.WebPages.Razor,
    Version=2.0.0.0

Edit(2):
In my /views/web.config (or /views/shared/web.config if I try @Paul 's solution below) in the element, I have my own base type which inherits from System.Web.Mvc.WebViewPage , the references in the containing library have been updated to MVC4 and go to definition takes me to the MVC4 dll.

Has anything changed in this area on the new release? I couldn't see anything pertinent in the release notes.

Best Answer

I ran into this issue with a Web Application - my .cshtml files got stuck on the System.Web.WebPages.WebViewPage base class when I needed the System.Web.Mvc.WebViewPage.

First, ensure your ~/Views/web.config file has the correct pageBaseType. In my case, I set to System.Web.Mvc.WebViewPage.

<configuration>
  <system.web.webPages.razor>
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
      <!-- ... -->
    </pages>
  </system.web.webPages.razor>
</configuration>

Then, importantly, some people have found this is key if the above is already good:

  1. Run a clean on the solution
  2. Unload the project with the issues
  3. Delete the .user file that Visual Studio generated next to the project.
  4. Reload the project with the issues
  5. Build the solution

For VS2015, the .user and .sln files have moved to the .vs hidden folder that is created next to the .sln file. However, from comments below, the error messages imply to me that the tooling is using the wrong version of MVC entirely, and deleting this folder does not fix the issue. To my knowledge, there is not a known solution.