Asp.net-Mvc

MVC Razor 視圖 Intellisense 在 VS 2013/2015/2017 中損壞

  • April 3, 2014

我有一個用 VS2010 編寫的現有項目,在 VS2010 中載入時效果很好。

當我在 VS2013 中載入同一個項目時,MVC Razor 視圖包含很多錯誤,就好像視圖文件夾中缺少配置文件一樣。

它似乎沒有使用根目錄和視圖文件夾中的配置文件正確載入 Razor 編輯器,而是給了我類似 …

The name 'model' does not exist in the current context

和 …

'System.Web.WebPages.Html.HtmlHelper' does not contain a definition for 'LabelFor' 
and no extension method 'LabelFor' accepting a first argument of type 
'System.Web.WebPages.Html.HtmlHelper' could be found 
(are you missing a using directive or an assembly reference?)

知道什麼會導致這種情況嗎?

編輯:根據要求配置文件….

從主 web.config 文件(不是全部,因為它太大而無法發布)

<runtime>
   <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
       <dependentAssembly>
           <assemblyIdentity name="Ninject" culture="neutral" publicKeyToken="c7192dc5380945e7" />
           <bindingRedirect newVersion="3.0.0.0" oldVersion="0.0.0.0-3.0.0.0" />
       </dependentAssembly>
       <dependentAssembly>
           <assemblyIdentity name="Microsoft.Practices.EnterpriseLibrary.Validation" culture="neutral" publicKeyToken="31bf3856ad364e35" />
           <bindingRedirect newVersion="5.0.505.0" oldVersion="0.0.0.0-5.0.505.0" />
       </dependentAssembly>
       <dependentAssembly>
           <assemblyIdentity name="System.Web" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
           <bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
       </dependentAssembly>
       <dependentAssembly>
           <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
           <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="3.0.0.0" />
       </dependentAssembly>
       <dependentAssembly>
           <assemblyIdentity name="Microsoft.Practices.ServiceLocation" publicKeyToken="31bf3856ad364e35" culture="neutral" />
           <bindingRedirect oldVersion="0.0.0.0-1.2.0.0" newVersion="1.2.0.0" />
       </dependentAssembly>
   </assemblyBinding>
</runtime>

“~/Views/”中的配置文件…

<?xml version="1.0"?>  
<configuration>
   <configSections>
       <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
           <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
           <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=1.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=3.0.0.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.Routing" />
               <add namespace="Emedia.Common.Mvc.Views.Helpers"/>
               <add namespace="Emedia.Common.Mvc.Views.Extensions"/>
       <add namespace="Emedia.Common.Utilities"/>
       <add namespace="Emedia.Common.Utilities.Extensions"/> 
       <add namespace="Emedia.Common.Mvc.Controllers.Helpers"/>
               <add namespace="Emedia.Resources.Service"/>
       <add namespace="Emedia.Subscriber.Controllers"/>
       <add namespace="Emedia.Subscriber.Controllers.ViewModels"/>
           </namespaces>
       </pages>
   </system.web.webPages.razor>

   <appSettings>
       <add key="webpages:Enabled" value="false" />
   </appSettings>

   <system.web>
       <httpHandlers>
           <add path="*" verb="*" type="System.Web.HttpNotFoundHandler"/>
       </httpHandlers>
       <pages
           validateRequest="false"
           pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
           pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
           userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
           <controls>
               <add assembly="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" namespace="System.Web.Mvc" tagPrefix="mvc" />
       </controls>
   </pages>
</system.web>

<system.webServer>
   <validation validateIntegratedModeConfiguration="false" />

   <handlers>
       <remove name="BlockViewHandler"/>
       <add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
   </handlers>
</system.webServer>

我考慮編輯@ChrisMoschini 的文章,但認為它已經足夠不同了。我的問題是我啟動了一個新的 MVC5 應用程序,並且盲目地從我想用作模板/起點的舊 MVC3 項目中複製了太多 web.config 設置。這樣做會導致我的 web.config 中引用了一些無效版本。

為了解決這個問題,我創建了另一個新的 MVC5 項目,並確保我的壞項目中的以下配置值與原始的、未修改的 MVC5 應用程序相匹配。同樣,不要盲目複製這些版本號。只需確保它們與您嘗試使用的版本的普通 MVC 應用程序匹配

在根web.config

<appSettings>
   ...
   <add key="webpages:Version" value="3.0.0.0"/> 
   ...
</appSettings>
<system.web>
   ...
   <compilation debug="true" targetFramework="4.5.1"/>
   <httpRuntime targetFramework="4.5.1"/>
   ...
</system.web>

Views\Web.config

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


<system.web.webPages.razor>
 ...
 <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.2.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
 ...
</system.web.webPages.razor>

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