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.
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:
- Run a clean on the solution
- Unload the project with the issues
- Delete the
.user
file that Visual Studio generated next to the project. - Reload the project with the issues
- 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.
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.
I tried all the solutions here but none of them worked for me. Again, my site runs fine but I don't have intellisense and get red wavy lines under a lot of things in my views that Visual Studio does not recognize, one of them being Html.BeginForm()
, as well as anything having to do with ViewBag
.
I'm working with a new MVC 5 project. After hours of comparing web.config lines, I finally found what fixed it for me.
My web.config in my root had the following line:
<system.web>
<compilation debug="true" targetFramework="4.5" />
<!-- ... -->
</system.web>
I compared to a previous project not using MVC 5, and copied over a block I noticed was missing from the new one, which was the following:
<system.web>
<compilation debug="true" targetFramework="4.5">
<assemblies>
<add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Web.Helpers, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Web.Mvc, Version=5.1.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Web.WebPages, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
</assemblies>
</compilation>
<!-- ... -->
</system.web>
I copied the above block over to my new project's web.config in the root, changing the versions to match the numbers for each assembly found in my project references (right-clicking each reference mentioned and selecting "Properties", "Version" is given at the bottom of the properties window for the selected reference).
After implementing the above, I now have intellisense and don't get any unknown red lines under things like Html.BeginForm
, ViewBag.Title
, etc.
I had run a project clean, and installed or reinstalled everything and was still getting lots of Intellisense errors, even though my site was compiling and running fine. Intellisense finally worked for me when I changed the version numbers in my web.config file in the Views folder. In my case I'm coding a module in Orchard, which runs in an MVC area, but I think this will help anyone using the latest release of MVC. Here is my web.config from the Views folder
<?xml version="1.0"?>
<configuration>
<configSections>
<sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
<section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
</sectionGroup>
</configSections>
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.1.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="Orchard.Mvc.ViewEngines.Razor.WebViewPage">
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
<add namespace="System.Linq" />
<add namespace="System.Collections.Generic" />
</namespaces>
</pages>
</system.web.webPages.razor>
<system.web>
<!--
Enabling request validation in view pages would cause validation to occur
after the input has already been processed by the controller. By default
MVC performs request validation before a controller processes the input.
To change this behavior apply the ValidateInputAttribute to a
controller or action.
-->
<pages
validateRequest="false"
pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=5.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL"
pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=5.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL"
userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=5.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<controls>
<add assembly="System.Web.Mvc, Version=5.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" namespace="System.Web.Mvc" tagPrefix="mvc" />
</controls>
</pages>
</system.web>
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<handlers>
<remove name="BlockViewHandler"/>
<add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
</handlers>
</system.webServer>
</configuration>