【问题标题】:VS2015 - IntelliSense not working in razor views in a class libraryVS2015 - IntelliSense 在类库中的剃刀视图中不起作用
【发布时间】:2015-09-14 09:48:25
【问题描述】:

我很难描述确切的问题,但这似乎“只是”一个仅在 razor (.cshtml) 视图中显示的 IntelliSense 问题。已经看了 2 天了,所以我真的需要一些帮助。

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

现在我添加了一个类库项目(默认项目,而不是“包”模板),并向其中添加了 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。类库中的视图被标记为嵌入式资源并使用虚拟路径提供程序加载。上面的案例是一个超级简化的可复现项目,在我和我2位同事的电脑上出现的症状是一样的。

如果我错过了重要信息,请尽管询问。

图像可视化问题

【问题讨论】:

  • 可能是由主web.config文件中的版本不匹配引起的
  • 添加了一张图片来显示问题的扩展。版本似乎都是正确的,这是一个新创建的 Web 应用程序 + 类库,因此没有进行任何升级。
  • 您的图像似乎显示了您所指的视图位于类库(而不是 Web 应用程序)中
  • This article 可能会有所帮助

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


【解决方案1】:

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

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

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

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

将这些更改为干净后,关闭解决方案,删除 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>

【讨论】:

  • 这个答案是错误的。你不需要对App.config 做任何事情(甚至有一个),它也没有提到你需要添加MVC (5.0) NuGet 包,引用System.Web.WebPages.Razor
  • 如果我删除了 app.config 或 MVC/Razor 相关设置,我添加了问题返回... 至于将 MVC 添加到项目中,这似乎很明显,因为在没有 MVC 的情况下使用 razor 并不经常有用...如果您有使用此解决方案未解决的其他问题,请为此打开一个新问题。
  • 为我工作,绝对完美。我们分别测试了每个步骤,结果您必须重新启动 Visual Studio。知道为什么吗?即使在进行了清理/构建等之后。
猜你喜欢
  • 2012-07-20
  • 2016-12-05
  • 2013-04-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-10
相关资源
最近更新 更多