17

I am having a hard time describing the exact issue, but is appears to be "just" an IntelliSense problem that only shows in razor (.cshtml) views. Been looking at this for 2 days now so I could really use some help.

I am using VS2015 Pro, started a new Web MVC application (WebApplication2 ) using the standard template. Inside that application everything works just fine.

Now I have added a class library project (the default one, not the "Package" template), and added the WebPages, MVC and razor nuget packages to it (in case that is relevant). The problem becomes visible as soon as I create a new view in the class library. It appears that all references to System libraries are unavailable in the cshtml file. With no files opened I get no errors at all, but when I open the view all system classes have the red squiggly line under them, and the Error list (Build + IntelliSense) suddenly contains a lot of errors for every system* library, example:

The type or namespace name 'Mvc' does not exist in the namespace 'System.Web' (are you missing an assembly reference?)

These include Linq, and the WebApplication2 (the test project), so not just System.Web.

Things that I have checked and done bases on similar answers on stack:

  • The web.config in the Views folder contains the correct versions, also tried just copying the one from the web application.
  • Reinstalled the nuget packages (Mvc, Razor, Webpages).
  • Resetting the userdata and removing the .vs folder.
  • Copy local is set to true on assemblies.

The actual case where we discovered this is our larger web application which was working fine in VS2010, but then we decided to upgrade to 2015 and .Net 4.6. The views in the class library are marked as embedded resource and loaded using a virtual path provider. The case above is a super simplified reproducible project, the symptoms are the same on my and my 2 colleagues' computers.

If I missed a crucial bit of information, please just ask.

Image visualizing the issue Image visualizing the issue

Martijn Kooij
  • 1,360
  • 13
  • 23
  • possible duplicate of [Need razor view engine auto-complete to work in a class library?](http://stackoverflow.com/questions/4158573/need-razor-view-engine-auto-complete-to-work-in-a-class-library) – Rik Sep 14 '15 at 09:54
  • Can be caused by version mismatches in the main `web.config` file –  Sep 14 '15 at 10:03
  • Added an image to show the extend of the issue. Versions all appear to be correct, this is a newly created web application + class library, so no upgrades took place. – Martijn Kooij Sep 14 '15 at 10:09
  • You image appears to show the view your referring to is in a class library (not the web application) –  Sep 14 '15 at 10:20
  • @StephenMuecke, Yes. That was the question indeed. Views inside a class library "trigger" this issue. – Martijn Kooij Sep 14 '15 at 10:24
  • 1
    [This article](https://conficient.wordpress.com/2013/11/27/asp-net-razor-views-in-class-libraries/) might help –  Sep 14 '15 at 10:41

1 Answers1

20

A reply in the article Stephen Muecke commented with got me started in the right direction. The solution is a mix of probably my confusion of which config needs to be where, and what feels like a workaround to something...

  1. Set the [Output Path] of your class library project to [bin/]. Mohammad Chehab mentions this in his (currently offline?) blog post which was referenced in this article: http://thetoeb.de/2014/01/05/enabling-mvc5-intellisense-in-a-classlibrary-project/

  2. In your [ClassLibrary/Views] folder you should have a Web.config which includes the correct razor versions and namespaces. I just copied the contents of the Web.config from our working Web application project and added/changed some namespaces. Example below.

  3. In your [ClassLibrary] root folder you should change the App.config so that it also contains the system.web section with the compilation setting. Example below.

After changing these to a clean, close the solution, delete the bin folders, open the solution and for me it finally all worked again. I do have a sporadic issue with System.Web.Mvc.xml being locked, perhaps a side effect of the output path change that MS did not foresee or something... Perhaps nothing to worry about.

Hope this helps some poor googling soul some day.

Project/Views/Web.config

<?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.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.webServer>
    <handlers>
      <remove name="BlockViewHandler"/>
      <add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
    </handlers>
  </system.webServer>

  <system.web>
    <compilation>
      <assemblies>
        <add assembly="System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
      </assemblies>
    </compilation>
  </system.web>
</configuration>

Project/App.config

<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=301880
  -->
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.6" />
  </system.web>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin.Security" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin.Security.OAuth" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin.Security.Cookies" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" />
        <bindingRedirect oldVersion="0.0.0.0-7.0.0.0" newVersion="7.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
      </dependentAssembly>
      <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.2.3.0" newVersion="5.2.3.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>
    </assemblyBinding>
  </runtime>
</configuration>
Adam Butler
  • 2,733
  • 23
  • 27
Martijn Kooij
  • 1,360
  • 13
  • 23
  • This answer is wrong. You don't need to do anything with `App.config` (or even have one), and it doesn't mention that you need to add the MVC (5.0) NuGet package, referencing `System.Web.WebPages.Razor`. – Jez Nov 03 '15 at 13:10
  • 1
    If I remove the app.config or the MVC/Razor related settings I added the problem returns... As for adding MVC to the project, that seemed obvious as using razor without MVC is not often useful... If you have a different problem that is not fixed using this solution, please open a new question for it. – Martijn Kooij Nov 04 '15 at 08:09
  • 1
    Worked for me, absolutely perfectly. We tested each step individually and it turns out you HAVE to restart Visual Studio. Any idea why? Even after doing a clean/build etc. – JARRRRG Oct 13 '16 at 16:36