asp.net-mvc - VS2015 - IntelliSense 无法在类库中的 Razor View 中工作

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

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

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

现在我添加了一个类库项目(默认项目,不是“Package”模板),并向其中添加了 WebPages、MVC 和 razor nuget 包(如果相关的话)。当我在类库中创建新 View 时,问题就变得明显。看来 cshtml 文件中对系统库的所有引用都不可用。 在没有打开文件的情况下,我根本没有收到任何错误,但是当我打开 View 时,所有系统类下面都有红色波浪线,并且错误列表(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。类库中的 View 被标记为嵌入式资源并使用虚拟路径提供程序加载。上面的案例是一个 super 简化的可重现项目,在我和我的 2 位同事的计算机上的症状是相同的。

如果我错过了重要的信息,请直接询问。

形象化问题的图像 Image visualizing the issue

最佳答案

Stephen Muecke 评论的文章中的回复让我朝着正确的方向开始。该解决方案可能是我对哪个配置需要在哪里的困惑,以及感觉像是某种解决方法的混合...

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

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

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

将这些更改为干净后,关闭解决方案,删除 bin 文件夹,打开解决方案,对我来说,它终于再次工作了。 我确实遇到了 System.Web.Mvc.xml 被锁定的偶发问题,这可能是 MS 未预见到的输出路径更改的副作用或其他原因......也许没什么好担心的。

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

项目/ View /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>

关于asp.net-mvc - VS2015 - IntelliSense 无法在类库中的 Razor View 中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32561779/

相关文章:

asp.net-mvc - Content 和 LoadContentFrom 之间的区别

c# - cshtml 元素中的 _layout 文件不会反射(reflect)在从它继承的文件中,除非在同一目录中

c# - 带有更新 2 的 Visual Studio 2015 - 'The Scc Display Information package did not load correctly'

msbuild - 升级 Web 服务器(服务器 2012 R2)后,Web 部署现在失败

asp.net-mvc - 没有 Controller 和操作的网址

asp.net-mvc - T4MVC 基础 Controller 没有默认构造函数

javascript - 当鼠标悬停在 HTML 表格单元格上时,我有哪些选项可以弹出日期列表?

javascript - backbone.js - 模板不会在 asp.net View 上呈现

c# - MVC3 Controller 如何检索 HTTPPOST 参数?

c - 如何管理从 matlab coder 自动生成的 c 文件?