构建错误 - 'System.Web.Mvc.ModelClientValidationRule' 冲突

23
我正在尝试在VS2010中“构建”我的MVC3 Web应用程序,但是一直遇到以下错误:
错误2:类型'System.Web.Mvc.ModelClientValidationRule'在'c:\Program Files(x86)\Microsoft ASP.NET\ASP.NET MVC 3\Assemblies\System.Web.Mvc.dll'和'c:\Program Files(x86)\Microsoft ASP.NET\ASP.NET Web Pages\v2.0\Assemblies\System.Web.WebPages.dll'中都存在。C:\Users\brownp\Documents\Visual Studio 2010\Projects\Cab\Cab\Models\AccountModels.cs 223行28列 Cab
此外,每次打开解决方案时,都会提示以下内容:

VS2010 error when opening solution

我通过Web平台安装程序进行安装,安装成功,但每次打开解决方案时都会出现相同的消息。
有人能提供一些指导吗?
谢谢,保罗
5个回答

44
在今天安装MVC4 beta之后,我的一些MVC 3项目无法编译。(ModelClientValidationRule冲突)解决方法如下:
编辑:
ProjectName.csproj

修改

<Reference Include="System.Web.WebPages"/> 

To

<Reference Include="System.Web.WebPages, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL "/>

1
我终于能够验证只需添加Version=1.0.0.0就足够了。因此,<Reference Include="System.Web.WebPages, Version=1.0.0.0" />。 - Tom Stickel
对于MVC5,请使用version=2.0.0.0。 - Manik Arora
在引用列表中选择 System.Web.WebPages,并将 Specific Version 属性更改为 False 对我有用。 - Evgeny Gorb
@EvgenyGorb -- 适用于MVC 3、4或5吗?...只是为了所有人的利益。谢谢 - Tom Stickel
@TomStickel - 我在 MVC 4 中使用它,但我想它也适用于其他版本。 - Evgeny Gorb
@EvgenyGorb NP,我注意到您在SO警报中的评论...而且由于我在4年前回答了这个问题,为了社区的利益,我想问一下你。谢谢! - Tom Stickel

13

好的,尝试这个解决方案...


  1. In the root Web.config file, add a new entry with the key webPages:Version and the value 1.0.0.0.

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

2.在“解决方案资源管理器”中,右键单击项目名称,然后选择“卸载项目”。然后再次右键单击名称,选择“编辑ProjectName.csproj”。

3.找到以下程序集引用:

    <Reference Include="System.Web.WebPages"/>
    <Reference Include="System.Web.Helpers" />

请将它们替换为以下内容:
<Reference Include="System.Web.WebPages, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL "/>
<Reference Include="System.Web.Helpers, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL "/>

4.保存更改,关闭您正在编辑的项目(.csproj)文件,然后右键单击项目并选择重新加载。

参考:http://forums.asp.net/t/1723108.aspx/1

同时尝试:http://www.asp.net/learn/whitepapers/mvc4-release-notes#_Toc303253815


10

从解决方案引用中删除 System.Web.WebPages 。就是这些了。


3
避免这种冲突的最好方法是-

  1. 进入解决方案资源管理器
  2. 引用
  3. 右键单击 System.Web.WebPages
  4. 删除

现在运行应用程序并享受吧!


0

这个问题与您在VS2010中描述的相同,在我的情况下发生在使用较新版本的MVC(V5)的VS2015中。

以下是我如何解决它的方法:

  • 将NUGET包更新到最新版本。

  • 在您的项目中,删除Microsoft.AspNet.WebPages的引用。然后,使用最新的包重新添加引用(使用“浏览...”):

    C:\Program Files (x86)\Microsoft ASP.NET\ASP.NET Web Pages\v2.0\Packages\Microsoft.AspNet.WebPages.2.0.30506.0\lib\net40

  • 确保所有项目都引用相同的程序集,如果不是,请按上述说明进行修复。然后,重新构建解决方案。在我的情况下,它修复了错误。

检查Web.config文件,并修复设置,例如:

<appSettings>
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="true" />
...
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
  <dependentAssembly>
    <assemblyIdentity name="System.Web.WebPages" 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="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
  </dependentAssembly>

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