Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio highlights non-existant errors in .cshtml pages using MVC5 and Razor

This question has been asked hundreds of times before, but none of the solutions presented seem to work.

I'm building an MVC5 project in Visual Studio 2013. It runs fine. However, in all the .cshtml pages in VS, all the inline Razor code is underlined in red, as though it's an error. @model and ViewBag state they do not exist in the current context. Html.ActionLink states that it does not contain a definition for ActionLink. There are other errors on different objects and the intellisense is broken.

These errors also show up when you build the project. Oddly, though, we've got a gated check-in on TFS and it builds just fine for that.

MVC and Razor are installed via NuGet. Nuget tells me that that version of MBC is 5.2.3 and the version of Razor installed is 3.2.3.

There's a web.config file in the Views folder, and it looks like this:

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

  <appSettings>
    <add key="webpages:Enabled" value="false" />
  </appSettings>

The main web.config files has the following section in it:

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

Things I have tried:

  • Updating the version of System.Web.Mvc.MvcWebRazorHostFactory to 5.2.3 doesn't fix the problem.

  • Changing the version of System.Web.Mvc in the main web.config to 5.2.0 doesn't fix the problem.

  • Updating the versions given for razor inside sectionGroup to 3.2.3 breaks the application.

  • Changing the value of webpages:Enabled to true in the web.config file in views and the main web.config doesn't fix the problem.

  • Deleting the .suo file, or the bin and object folders (in combination with the above) doesn't fix the problem.

  • Installing Asp.NET Web Helpers Library from Nuget doesn't fix the problem.

  • Adding a system.web section to the Views\web.Config with values for pages and controls keyed to MVC version 5.2.3 doesn't fix the problem.

What on earth else can I try?

like image 446
Bob Tway Avatar asked May 22 '15 09:05

Bob Tway


1 Answers

Try changing the web.config in your Views folder to use these versions:

System.Web.WebPages.Razor, Version=3.0.0.0
System.Web.Mvc, Version=5.2.3.0

The assembly binding re-direct indicates you are using Mvc version 5.2.3, not 5.2.0

I just had a similar problem and that fixed it. I am using the latest versions on NuGet as of 5/29/15 and just upgraded from MVC3.

The Razor NuGet package is Microsoft.AspNet.Razor.3.2.3, but the binary is using this name: System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35

Here is my web.config in the views folder:

<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>

    <!--
        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.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>
like image 107
Michael Finn Avatar answered Sep 28 '22 12:09

Michael Finn