从 Asp.Net 4.7 项目引用 .Net Standard 2.0 库时出错

Error when referencing .Net Standard 2.0 library from Asp.Net 4.7 project

我有一个针对 asp.net 4.7 的 MVC 项目。 我创建了一个 class 库,它针对 .Net 标准 2.0

如果我从我的 MVC 项目中引用 class 库,我会在应用程序启动时遇到以下异常。

System.MissingMethodException: 'Method not found:

Message "Method not found: 'System.Collections.ObjectModel.Collection`1 System.Web.Http.HttpConfiguration.get_MessageHandlers()'."

GlobalConfiguration.Configure(WebApiConfig.Register);

如果我删除引用,一切都会恢复正常。 有什么想法可能是错误的吗?

如果您的 mvc 项目以 .net 4.6.1 为目标,您可以使用 .net 标准 2.0 class 库。

您需要将此绑定重定向添加到 web.config:

<dependentAssembly>
   <assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
   <bindingRedirect oldVersion="0.0.0.0-4.2.0.0" newVersion="4.2.0.0" />
</dependentAssembly>