Visual Studio智能感知出现问题

3

我刚开始学习MVC4和C#,已经设计了这个网站大约两周了,Intelisense一直没有任何问题。

从两天前开始,Visual Studio告诉我@Viewbag和其他@命令不是我的项目的一部分,我可能缺少某些东西,或者它告诉我Viewbag在当前上下文中不存在。

我不确定如何解决这个问题(我已经重新启动了项目和工作机器),但仍然显示为下划线,并告诉我它在上下文中不存在。

由于我还很新,所以当我查看/尝试新代码时,这非常困难,因为所有都是错误的...

示例图片:

enter image description here enter image description here enter image description here enter image description here 你几乎看不清的长错误声明为

System.Web.WebPages.Html.htmlHelper' does not contain a definition for 'Actionlink' .... missing assembly reference ?

我在Razor视图中的任何@代码中也会出现此错误,除了C# @。

WebConfig文件

<?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=169433
 -->
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit   http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<connectionStrings>
<add name="DefaultConnection" connectionString="Server=ANE-SQL\ANESQLSERVER;Database=OilGas;User Id=software;Password=GLvp$102;" providerName="System.Data.SqlClient" />
</connectionStrings>
<appSettings>
 <add key="webpages:Version" value="2.0.0.0" />
 <add key="webpages:Enabled" value="false" />
 <add key="PreserveLoginUrl" value="true" />
 <add key="ClientValidationEnabled" value="true" />
 <add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
<system.web>

<customErrors mode="RemoteOnly"></customErrors>


<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
<authentication mode="Forms">
  <forms loginUrl="~/Account/Login" timeout="180" />
</authentication>
<pages>
  <namespaces>
    <add namespace="System.Web.Helpers" />
    <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" />
    <add namespace="System.Web.WebPages" />
  </namespaces>
</pages>



</system.web>
<system.webServer>
<!--<modules runAllManagedModulesForAllRequests="true" />-->
<validation validateIntegratedModeConfiguration="false" />
<handlers>
  <remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
  <remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
  <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
  <add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*."   verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
   <add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll"   preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
  <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*."   verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS"    type="System.Web.Handlers.TransferRequestHandler"    preCondition="integratedMode,runtimeVersionv4.0" />
  </handlers>
  </system.webServer>
  <runtime>
  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
  <dependentAssembly>
    <assemblyIdentity name="DotNetOpenAuth.Core" publicKeyToken="2780ccd10d57b246" />
    <bindingRedirect oldVersion="1.0.0.0-4.0.0.0" newVersion="4.1.0.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="DotNetOpenAuth.AspNet" publicKeyToken="2780ccd10d57b246" />
    <bindingRedirect oldVersion="1.0.0.0-4.0.0.0" newVersion="4.1.0.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
    <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
    <bindingRedirect oldVersion="1.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
    <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
    <bindingRedirect oldVersion="1.0.0.0-1.3.0.0" newVersion="1.3.0.0" />
    </dependentAssembly>
    </assemblyBinding>
   </runtime>
    <entityFramework>
    <defaultConnectionFactory    type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
  <parameters>
    <parameter value="v11.0" />
     </parameters>
    </defaultConnectionFactory>
  </entityFramework>
    </configuration>

你还能编译和运行你的项目吗? - rhughes
你使用的是哪个版本和版本号的Visual Studio和Windows操作系统? - rhughes
VS 2012 Prem,7 Professional - Pakk
3个回答

2

请确保在您的web.config文件的appSettings中指定了您正在使用的Razor版本:

<appSettings>
    <add key="webpages:Version" value="2.0.0.0" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>

同时确保你的视图文件位于标准位置(也就是~/Views文件夹)。


将其更改为您上面展示的代码,但不起作用,甚至尝试设置--<add key="webpages:Enabled" value="true" />--,没有效果,我的所有视图都在../Views中,让我发布我的web.config文件,看看是否有不属于它的内容? - Pakk
在我的情况下,将 <add key="webpages:Version" value="2.0.0.0" /> 修改为 <add key="webpages:Version" value="3.0.0.0" /> 解决了问题。在我的电脑上安装了 VS 2015,并且在新的 Web 项目中 Razor 智能感知功能起作用,但在旧项目中则不行。其中的区别在于 web.config 文件中的 webpages:Version。 - andrew

1
我曾经遇到过同样的问题,因为错误地将<appSettings>添加到视图Web.config中而不是“主”Web.config文件中。花了我几个小时才弄清楚这一点......希望能帮助到有需要的人。

1
创建一个新的互联网Web应用程序,取出您的更改(从旧代码中),插入新创建的互联网Web应用程序配置文件,添加旧更改(跳过逐行阅读,看看是否真的会有任何问题)。
希望这可以帮助。

我刚刚发现appSettings需要放在Web.config文件的顶部,否则它会让Intellisense出问题。我把我的放在<configSections>元素下面(因为这是IIS要求的第一个元素)。 - Steven

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接