VS2015 - IntelliSense无法在类库中的剃刀视图中工作

时间:2015-09-14 09:48:25

标签: asp.net-mvc razor visual-studio-2015 intellisense

我很难描述确切的问题,但似乎只是“只是”智能感知问题,只能在剃刀(.cshtml)视图中显示。已经看了2天了,所以我真的可以帮忙。

我正在使用VS2015 Pro,使用标准模板启动了一个新的Web MVC应用程序(WebApplication2)。在该应用程序内部,一切正常。

现在我添加了一个类库项目(默认的一个,而不是“Package”模板),并添加了WebPages,MVC和razor nuget包(如果相关的话)。一旦在类库中创建新视图,问题就会变得明显。似乎所有对系统库的引用都在cshtml文件中不可用。 没有打开文件我根本没有错误,但是当我打开视图时,所有系统类都有红色波浪线,并且错误列表(Build + IntelliSense)突然包含每个系统*库的大量错误,例如:

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

这些包括Linq和WebApplication2(测试项目),所以不仅仅是System.Web。

我检查和完成的事情基于堆栈上的类似答案:

  • Views文件夹中的web.config包含正确的版本,也尝试从Web应用程序中复制一个版本。
  • 重新安装了nuget软件包(Mvc,Razor,网页)。
  • 重置用户数据并删除.vs文件夹。
  • 复制本地在程序集上设置为true。

我们发现这个的实际情况是我们的大型Web应用程序在VS2010中运行良好,但后来我们决定升级到2015和.Net 4.6。类库中的视图标记为嵌入式资源,并使用虚拟路径提供程序加载。上面的案例是一个超简化的可重复项目,我和我的两个同事的计算机上的症状是相同的。

如果我错过了一些关键信息,请问问。

图像可视化问题 Image visualizing the issue

1 个答案:

答案 0 :(得分:18)

Stephen Muecke评论的文章中的回复让我开始朝着正确的方向前进。解决方案可能是我的混淆,其中配置需要在哪里,以及什么感觉就像某种解决方法......

  1. 将类库项目的[输出路径]设置为[bin /]。 Mohammad Chehab在他的(当前离线?)博客文章中提到了这一点,该文章在本文中引用:http://thetoeb.de/2014/01/05/enabling-mvc5-intellisense-in-a-classlibrary-project/

  2. 在[ClassLibrary / Views]文件夹中,您应该有一个Web.config,其中包含正确的剃刀版本和命名空间。我刚从我们正在运行的Web应用程序项目中复制了Web.config的内容,并添加/更改了一些名称空间。以下示例。

  3. 在[ClassLibrary]根文件夹中,您应该更改App.config,以便它还包含带有编译设置的system.web部分。以下示例。

  4. 将这些更改为干净后,关闭解决方案,删除bin文件夹,打开解决方案,对我而言,它最终全部工作了。 我确实有一个零星的问题,System.Web.Mvc.xml被锁定,可能是MS没有预见到的输出路径变化的副作用......或许没有什么可担心的。

    希望有一天能帮助一些可怜的谷歌搜索灵魂。

    项目/查看/ 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>
    

    项目/ 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>
    
相关问题