Asp.net-Mvc

VS2015 - IntelliSense 在類庫中的剃刀視圖中不起作用

  • October 3, 2017

我很難描述確切的問題,但似乎“只是”一個僅在 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 位同事的電腦上的症狀是相同的。

如果我錯過了重要的資訊,請詢問。

形象化問題的圖像 形象化問題的圖像

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 沒有預見到的輸出路徑更改的副作用或其他什麼……也許沒什麼好擔心的。

希望有一天這能幫助一些可憐的Google搜尋靈魂。

項目/視圖/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>

引用自:https://stackoverflow.com/questions/32561779