Razor Views does not see System.Web.Mvc.HtmlHelper

Now I am upgrading to MVC4. I followed the instructions http://www.asp.net/whitepapers/mvc4-release-notes#_Toc303253806 , but I have errors in my Razor views and layouts, for example

  • 'System.Web.WebPages.Html.HtmlHelper' does not contain a definition for "BeginRouteForm" and no extension method "BeginRouteForm" Accepting the first argument of the type 'System.Web.WebPages.Html.HtmlHelper' can be found (you are missing the using directive or the link to assembly?)

I also have errors like:

  • The name "View Bag" does not exist in the current context

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

Not quite sure what other information is relevant, but I'm here:

  • I have not updated my class libraries, etc. in sln before .net 4.5.
  • The project was created in VS2010, but I am migrating to VS2012
  • The project opens and works OK with VS2012, .Net 4.0, MVC 3

Any pointers appreciated.

Edit: All links and files in web.config have been updated 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 the @Paul solution below) in the element, I have my own base type, which inherits from System.Web. Mvc.WebViewPage, the link to the containing library has been updated to MVC4, and going to the definition will lead me to the MVC4 DLL.

Has something changed in this area in the new release? In the release notes, I did not see anything suitable.

+58
c # razor asp.net-mvc-4
Aug 20 '12 at 16:40
source share
21 answers

I ran into this problem with a web application - my .cshtml files .cshtml stuck in the base class System.Web.WebPages.WebViewPage when I needed System.Web.Mvc.WebViewPage .

First, make sure your ~ / Views / web.config file has the correct pageBaseType . In my case, I installed 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 that this is the key, if the above is already good:

  • Run cleanup in solution
  • Unload the project with problems
  • Delete the .user file created by Visual Studio next to the project.
  • Restart project with problems
  • Create a solution

For VS2015, the .user and .sln files moved to the hidden .vs folder created next to the .sln file. However, from the comments below, the error messages imply that the toolkit is fully using the wrong version of MVC, and deleting this folder does not fix the problem. As far as I know, there is no known solution.

+83
Apr 30 '14 at 13:36 on
source share

I had the same problem when upgrading to MVC 5, and it was resolved by updating 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 has been installed in version: 4.0.0.0 I hope this helps someone.

+22
Feb 02 '14 at 19:31
source share

I tried all the solutions here, but none of them worked for me. Again, my site is working fine, but I do not have intellisense and red wavy lines in many things in my views that Visual Studio does not recognize, one of them is Html.BeginForm() , and also something related to the ViewBag .

I am working with a new MVC 5 project. After hours of comparing web.config strings, I finally found that it fixed it for me.

My web.config of my root had the following line:

 <system.web> <compilation debug="true" targetFramework="4.5" /> <!-- ... --> </system.web> 

I compared with the previous project not using MVC 5, and copied over the block, which, as I noticed, was missing in the new one, which was as follows:

 <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 to my new web.config project in the root, changing the versions according to the numbers for each assembly found in my project links (by right-clicking on each link and selecting "Properties", the "Version" is given at the bottom parts of the properties window for the selected link).

After implementing the above, I now have intellisense and am not getting any unknown red lines under things like Html.BeginForm , ViewBag.Title , etc.

+7
Apr 13 '14 at 2:28
source share

I executed the project clean and installed or reinstalled everything and still got a lot of Intellisense errors, although my site compiled and worked fine. Intellisense finally worked for me when I changed the version numbers in the web.config file in the Views folder. In my case, I am coding a module in Orchard that works in the MVC area, but I think this will help anyone using the latest version 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> 
+5
May 05 '14 at
source share

You need to copy Views/Web.config to /Shared . This will allow Razor to use the base MVC type and parser. You can read more here: http://blog.slaks.net/2011/02/dissecting-razor-part-3-razor-and-mvc.html

+4
Aug 20 2018-12-18T00:
source share

I dealt with this problem after upgrading from Visual Studio 2013 to Visual Studio 2015 . Having tried most of the recommendations found in this and other similar SO posts, I finally found a problem. The first part of the fix was to upgrade all of my NuGet materials to the latest version (you might need to do this in VS13 if you encounter a NuGet error), after which I had to fix the versions listed in the Web.config View Web.config . It includes:

  • Fix MVC versions and its child libraries in the new version (expand References , then right-click on Sytem.Web.MVC , then Properties to get your version)
  • Correct the version of Razor .

Mine looked like this:

 <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.2.3.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> </system.web.webPages.razor> <appSettings> <add key="webpages:Enabled" value="false" /> </appSettings> <system.web> <httpHandlers> <add path="*" verb="*" type="System.Web.HttpNotFoundHandler"/> </httpHandlers> <pages validateRequest="false" pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"> <controls> <add assembly="System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" 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> 
+3
Aug 18 '15 at 4:19
source share

Having tried everything in vain, I found that in my case it did not work due to an incorrect attribute value in the csproj web project file. When I change ToolsVersion to 14 , which matches my current version of the IDE (i.e. Visual Studio 2015), everything works like a charm:

 <?xml version="1.0" encoding="utf-8"?> <Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition= ..... 
+2
Sep 10 '15 at 19:15
source share

For those of you who suffered after migrating the project from VS 2013 to VS 2015, I was able to fix this problem by installing the ASP.NET tools update from https://visualstudiogallery.msdn.microsoft.com/c94a02e9-f2e9-4bad-a952 -a63a967e3935 / file / 77371/6 / AspNet5.ENU.RC1_Update1.exe? SRC = VSIDE & UPDATE = TRUE .

+2
Jan 07 '16 at
source share

To deploy Matt DeKrey's answer, just deleting the csproj.user file (without having to re-create the solutions) managed to solve the problem for me.

The only side effect that I had was that I needed to reset “Start action” in order to use a specific page.

+1
Nov 03 '14 at 4:22
source share

In the world of contracts, I often use machines that use old images. After trying all the above, I decided to upgrade VS 2013 to the latest version (Update 4). After 90 minutes and reloading links work fine! Hope this helps!

+1
Apr 28 '15 at 16:44
source share

Recently, the same problem has appeared, and here is how I fixed it: In Visual Studio with an open project, Goto:

  • Tools -> NuGet Package Manager -> Manage NuGet packages for the solution.
  • In the window that opens, select Updates . then click Update All .

It will load what is missing in your project, and everything should return to the right place.

+1
Oct 12 '17 at 10:27
source share

Just started to study the problem myself, and this is what it looks like in my case. If you have the correct values ​​in your web configuration, then this is just an error in MVC4. http://connect.microsoft.com/VisualStudio/feedback/details/727729/viewbag-not-recognized-in-asp-net-mvc-4-project

0
Jul 30 '13 at 21:37
source share
 *<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>* 

This configuration is missing, add it and install the appropriate version of assemblies

0
Aug 10 '15 at 18:24
source share

As an option on the topic, I could swear that my views of \ Web.config are correct:

 <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /> 

But I really needed to refer to Version=4.0.0.1 , not Version=4.0.0.0 because of this security update, which was dropped some time ago.

0
Oct 02 '15 at 20:23
source share

For me, it was a stupid deployment error: web projects can have more than one web.config. He worked on a development machine, not on production, but we did not understand that the deployment of the script only captured the Web.config file in the root directory and did not copy the Web.config file to the Views folder.

0
May 30 '16 at 12:08
source share

I ran into this particular problem; no (or incorrect) intellisense showing when trying to use razor tags. My particular problem was that VS2015 complained that Html.BeginForm did not exist in the current context.

I have areas created in my MVC project and managed to narrow down the cause of my error in the web.config file for a specific area, and not for the global web.config.

It turns out that the cause of this problem for me was that I added an SQL connectionString to web.config for the area that didn't work, this (I guess) caused a parsing error, but the project was correctly drawn up.

Moving connectionString to the global web.config fixed the problem. Hope this can be helpful to others.

0
Jun 07 '16 at 12:03
source share

My situation only happened after I created a custom class called BaseViewPage that overrides the WebViewPage class. I first added the following to my main Web.confg file:

 <pages pageBaseType="ZooResourceLibrary.Web.Support.BaseViewPage"> 

And the same with folders. View : web.config file:

 <pages pageBaseType="ZooResourceLibrary.Web.Support.BaseViewPage"> 

I tried many other answers, and nobody did, still letting me save the BaseViewPage class. I fixed this to remove the pageBaseType attribute only from the Main Web.config file. Save it in View web.config.

0
Jan 18 '17 at 16:58
source share

I met several answers in SO, and in the end I realized that my mistake was that I had the error "Html.TextBoxFor". In my case, I wrote "Html.TextboxFor". I did not head B in TextBoxFor. This is fixed and voila. The problem is solved. Hope this helps someone.

0
Feb 23 '17 at 21:30
source share

I used the following article on MSDN to solve this problem (in this case, from MVC 4 to MVC 5)

https://docs.microsoft.com/en-us/aspnet/mvc/overview/releases/how-to-upgrade-an-aspnet-mvc-4-and-web-api-project-to-aspnet-mvc- 5-and-web-api-2

Key excerpts from the article:

The main Web.config file (not the one in the Views folder)

In the updated block update System.Web.Mvc to 5.0.0.0, System.Web.Helpers to 3.0.0.0 and System.Web.WebPages to 3.0.0.0

 <dependentAssembly> <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" /> <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" /> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" /> <bindingRedirect oldVersion="1.0.0.0-5.0.0.0" newVersion="5.0.0.0" /> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" /> <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" /> </dependentAssembly> 

Then find the appsettings block and refresh the web pages: version value to 3.0.0.0

 <appSettings> <add key="webpages:Version" value="3.0.0.0" /> </appSettings> 

Web.config Views

Update the host factoryType block for System.Web.Mvc to 5.0.0.0

 <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" /> </namespaces> </pages> </system.web.webPages.razor> 

Then, under the block block, update any links to System.Web.Mvc to 5.0.0.0

  <pages   validateRequest="false"   pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"   pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"   userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">  <controls>   <add assembly="System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" namespace="System.Web.Mvc" tagPrefix="mvc" />  </controls> </pages> 

And finally, when updating the System.Web link configuration block to 3.0.0.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> 
0
Jun 14 '17 at 2:31 on
source share

That's right, I tried to fix this problem for a while. I used all the solutions in various questions on this issue, and none of them worked.

I just solved the problem this morning. Once you have installed the web.config file for the project and views, make sure that all versions of the .dll correspond to what you have in the links folder. You will need to unload the project, edit the .csproj file, and then update all versions of the .dll in this file.

 System.Web.Helpers System.Web.Mvc System.Web.WebPages 

Hope this helps, as I finally fixed this issue! No more red squiggly lines.

This also fixed the context menu problem that I encountered, I did not get the opportunity to add a controller, view, etc.

0
Nov 18 '17 at 10:17
source share

For me, the solution was to change the following:

  <pages validateRequest="false" pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"> <controls> <add assembly="System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" namespace="System.Web.Mvc" tagPrefix="mvc" /> </controls> </pages> 
-one
Feb 22 '16 at 9:36
source share



All Articles