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