Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC Razor view Intellisense broken in VS 2013/2015/2017

I have an existing project written in VS2010 which when loaded in VS2010 works perfectly.

When I load this same project in VS2013 the MVC Razor views contain lots of errors as if the config file is missing from the views folder.

It appears to have not loaded the Razor editor correctly using the config files from both the root and the views folder and instead gives me errors like ...

The name 'model' does not exist in the current context 

and ...

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

...

Any idea what would cause this?

Edit: Config files as requested ....

From main web.config file (not all of it as it's way too big to post)

<runtime>     <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">         <dependentAssembly>             <assemblyIdentity name="Ninject" culture="neutral" publicKeyToken="c7192dc5380945e7" />             <bindingRedirect newVersion="3.0.0.0" oldVersion="0.0.0.0-3.0.0.0" />         </dependentAssembly>         <dependentAssembly>             <assemblyIdentity name="Microsoft.Practices.EnterpriseLibrary.Validation" culture="neutral" publicKeyToken="31bf3856ad364e35" />             <bindingRedirect newVersion="5.0.505.0" oldVersion="0.0.0.0-5.0.505.0" />         </dependentAssembly>         <dependentAssembly>             <assemblyIdentity name="System.Web" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />             <bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />         </dependentAssembly>         <dependentAssembly>             <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />             <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="3.0.0.0" />         </dependentAssembly>         <dependentAssembly>             <assemblyIdentity name="Microsoft.Practices.ServiceLocation" publicKeyToken="31bf3856ad364e35" culture="neutral" />             <bindingRedirect oldVersion="0.0.0.0-1.2.0.0" newVersion="1.2.0.0" />         </dependentAssembly>     </assemblyBinding> </runtime> 

Config file in "~/Views/" ...

<?xml version="1.0"?>   <configuration>     <configSections>         <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">             <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />             <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=1.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=3.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.Routing" />                 <add namespace="Emedia.Common.Mvc.Views.Helpers"/>                 <add namespace="Emedia.Common.Mvc.Views.Extensions"/>         <add namespace="Emedia.Common.Utilities"/>         <add namespace="Emedia.Common.Utilities.Extensions"/>          <add namespace="Emedia.Common.Mvc.Controllers.Helpers"/>                 <add namespace="Emedia.Resources.Service"/>         <add namespace="Emedia.Subscriber.Controllers"/>         <add namespace="Emedia.Subscriber.Controllers.ViewModels"/>             </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=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"             pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"             userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">             <controls>                 <add assembly="System.Web.Mvc, Version=3.0.0.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> 

like image 372
War Avatar asked Apr 03 '14 08:04

War


People also ask

Which is better razor or MVC?

From the docs, "Razor Pages can make coding page-focused scenarios easier and more productive than using controllers and views." If your ASP.NET MVC app makes heavy use of views, you may want to consider migrating from actions and views to Razor Pages.

What is the difference between razor and MVC?

A Razor Page is almost the same as ASP.NET MVC's view component. It has basically the syntax and functionality same as MVC. The basic difference between Razor pages and MVC is that the model and controller code is also added within the Razor Page itself. You do not need to add code separately.

What is MVC Razor view?

Razor is a markup syntax that lets you embed server-based code into web pages using C# and VB.Net. It is not a programming language. It is a server side markup language. Razor has no ties to ASP.NET MVC because Razor is a general-purpose templating engine. You can use it anywhere to generate output like HTML.

Can you mix MVC and Razor pages?

You can add support for Pages to any ASP.NET Core MVC app by simply adding a Pages folder and adding Razor Pages files to this folder. Razor Pages use the folder structure as a convention for routing requests.


2 Answers

I considered editing @ChrisMoschini's post, but thought it was different enough. My issue was that I started a new MVC5 application, and blindly copied over too many web.config settings from an old MVC3 project I wanted to use as a template/starting point. Doing this caused me to have some invalid versions referenced in my web.config.

To fix, I created another new MVC5 project and made sure the following config values in my bad project matched the vanilla, unmodified MVC5 app. Again, do not blindly copy these version numbers. Just make sure they match a vanilla MVC app of the version that you're trying to get to work

in root web.config:

<appSettings>     ...     <add key="webpages:Version" value="3.0.0.0"/>      ... </appSettings> <system.web>     ...     <compilation debug="true" targetFramework="4.5.1"/>     <httpRuntime targetFramework="4.5.1"/>     ... </system.web> 

in the Views\Web.config:

<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>   <system.web.webPages.razor>   ...   <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.2.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />   ... </system.web.webPages.razor> 
like image 173
viggity Avatar answered Sep 28 '22 12:09

viggity


A few of my projects had this issues for months. I tried so many workarounds and nothing worked. The issues seemed to be with the projects: the same project will have Intellisense issue across all PCs with Visual Studio. Finally I came through this post: http://www.dennisonpro.info/managing-intellisense-in-razor-views-with-mvc-5-using-custom-builds-in-visual-studio-2013/

In our case the cause was the output directory of all those projects were changed to other than "bin\". By changing the OutputPath back to "bin\" (and cleaning the project, closing then re-opening Visual Studio), I got Intellisense back.

The post referenced above also provided a workaround to maintain a separate output directory while still having Intellisense.

Hope this helps someone someday.

like image 28
Jim Avatar answered Sep 28 '22 10:09

Jim