构建MVC3解决方案进行得很好,但在浏览器中有一个错误:

编译器错误信息:CS0234:类型或命名空间名称“Html”在命名空间“System.Web”中不存在。Mvc'(您是否缺少一个程序集引用?)

Source Error:
Line 25:         <add namespace="System.Web.Mvc" />
Line 26:         <!--<add namespace="System.Web.Mvc.Ajax" />-->
Line 27:         <add namespace="System.Web.Mvc.Html" />
Line 28:         <add namespace="System.Web.Routing" />
Line 29:         <add namespace="System.Web.WebPages" />

我已经用NuGet安装了解决方案的包,并为所有项目设置了MVC3。MVC3是否包含库System.Web.Mvc。Ajax, System.Web.Mvc.Html等等?为什么我得到错误?


在引用文件夹中,我有System.Web.Mvc

运行时版本:v4.0.30319,

Version: 3.0.0.0

. 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=152368
  -->
<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>
  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
    <add key="webpages:Version" value="1.0.0.0" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <authentication mode="Forms">
      <forms loginUrl="~/Account/LogOn" timeout="2880" />
    </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.Routing" />
        <add namespace="System.Web.WebPages" />
      </namespaces>
    </pages>
    <httpRuntime targetFramework="4.5" encoderType="System.Web.Security.AntiXss.AntiXssEncoder, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
    <machineKey compatibilityMode="Framework45" />
    <profile defaultProvider="DefaultProfileProvider">
      <providers>
        <add name="DefaultProfileProvider" type="System.Web.Providers.DefaultProfileProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" applicationName="/" />
      </providers>
    </profile>
    <membership defaultProvider="DefaultMembershipProvider">
      <providers>
        <add name="DefaultMembershipProvider" type="System.Web.Providers.DefaultMembershipProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/" />
      </providers>
    </membership>
    <roleManager defaultProvider="DefaultRoleProvider">
      <providers>
        <add name="DefaultRoleProvider" type="System.Web.Providers.DefaultRoleProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" applicationName="/" />
      </providers>
    </roleManager>
    <sessionState mode="InProc" customProvider="DefaultSessionProvider">
      <providers>
        <add name="DefaultSessionProvider" type="System.Web.Providers.DefaultSessionStateProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" />
      </providers>
    </sessionState>
  </system.web>
  <system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
  </system.webServer>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
  </entityFramework>
  <connectionStrings>
    <add name="EFDbContext" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=SportStore;Integrated Security=True;Pooling=False;" providerName="System.Data.SqlClient" />
  </connectionStrings>
</configuration>

System.Web.Mvc命名空间

文件packages.config:

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="EntityFramework" version="5.0.0-rc" />
  <package id="jQuery" version="1.7.2" />
  <package id="jQuery.UI.Combined" version="1.8.11" />
  <package id="jQuery.Validation" version="1.9" />
  <package id="Microsoft.AspNet.Mvc" version="3.0.20105.1" />
  <package id="Microsoft.AspNet.Providers.Core" version="1.0" />
  <package id="Microsoft.AspNet.Providers.LocalDB" version="1.0" />
  <package id="Microsoft.AspNet.Razor" version="1.0.20105.408" />
  <package id="Microsoft.AspNet.WebPages" version="1.0.20105.408" />
  <package id="Microsoft.Web.Infrastructure" version="1.0.0.0" />
  <package id="Modernizr" version="2.0.6" />
  <package id="Moq" version="4.0.10827" />
  <package id="Ninject" version="3.0.1.10" />
</packages>

当前回答

好吧,很多人似乎都经历过这个问题。我的问题是由于引用具有指向其他项目的bin文件夹的路径,或者引用来自系统文件夹的dll。这个项目已经有6年的历史了,之前的开发人员决定用这种方式引用他们的库。

解决方案是遍历每个引用并检查路径,然后删除引用并使用NuGet添加它。对于任何不在NuGet上的包,我在包旁边创建了另一个文件夹,并把dll放在那里,以与NuGet相同的布局。

我还必须检查配置文件,确保所使用的软件包版本正确。一个漫长而痛苦的过程!

其他回答

我有同样的问题,没有一个解决方案为我工作,最后我删除了System.Web.MVC,并再次添加,然后一切恢复正常,我的问题解决了。

我也有同样的问题,但上面的方法对我都没用。不过他们确实帮我找到了正确的方向。

例如,当我将System.Web.Mvc引用的“Copy Local”设置为“true”时,它会自动将其设置为False。

我有多个项目依赖于我的解决方案中的System.Web.Mvc引用,但只有一个导致了这个问题。 在VS 2012中,这个引用被标记为黄色的注意三角形。

找到这个引用=>删除它=>重新添加它

这为我解决了问题。 希望这能有所帮助

不管出于什么原因,我的解决方案中有两个web项目以某种方式自发地卸载asp.net MVC。我从Nuget安装了它,现在它们都重新工作了。这发生在最近一批windows更新之后,其中包括我正在使用的版本(4.5.1)的.net框架更新。

编辑:来自。net Web开发和工具博客:

微软的Asp。Net MVC安全更新MS14-059打破了我的构建!

很可能将DLL设置为CopyLocal/true或任何其他主要修复程序将解决您的问题,但这里有另一个边缘情况,让我浪费了20分钟的时间。

当您将名称空间添加到Views/Web时。配置,确保它们的大小写正确:

  <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="THE.NameSpace" />
        <add namespace="THE.Namespace" />
      </namespaces>
    </pages>
  </system.web.webPages.razor>

我做了一件非常愚蠢的事(也许你也做了)。

我试图调用system。web。mvc。html。部分(“< >部分页")

System.Web.Mvc.Html是一个命名空间,而不是一个类,我没有读我的错误消息那么好,所以我解释我的错误类Html不存在于命名空间System.Web.Mvc,这就是我如何结束在这里(愚蠢的我知道)。

我所需要做的就是添加一个using语句@using System.Web.Mvc.Html到我的页面,然后@Html。部分工作如预期。