Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Razor Views not seeing System.Web.Mvc.HtmlHelper

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.

like image 764
NikolaiDante Avatar asked Aug 20 '12 16:08

NikolaiDante


2 Answers

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.

like image 76
Matt DeKrey Avatar answered Sep 22 '22 03:09

Matt DeKrey


I had the same issue when updating to MVC 5 and it was solved by updating the web.config inside the Views folder.

<system.web.webPages.razor> <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /> <pages pageBaseType="System.Web.Mvc.WebViewPage">   <namespaces>     <add namespace="System.Web.Mvc" />     <add namespace="System.Web.Mvc.Ajax" />     <add namespace="System.Web.Mvc.Html" />     <add namespace="System.Web.Optimization"/>     <add namespace="System.Web.Routing" />   </namespaces> </pages> 

The host -> factoryType was set to version:4.0.0.0 hope this helps anyone.

like image 23
user3141889 Avatar answered Sep 21 '22 03:09

user3141889