我已经验证了将 HttpResponseMessage
作为返回类型的文件设置为“编译”,就像他们在这里所说的:Why I cannot use HttpResponseMessage class?
我已经从 packages.config 迁移到 packagereference (nuget) 并且有一个包含 webapi 的 .net 4.7.2 mvc 项目。它以前工作过,但在几次 Nuget 更新后它不再工作了。我在 system.web.http 和框架中的 4.0.0 版本构建以及较新的 4.x nuget 方面遇到了问题。
我的问题似乎与 https://github.com/Microsoft/dotnet/issues/737 有关但我不确定。
我也试过这个https://stackoverflow.com/a/50095678/169714
这是我的 web.config 的一部分:
<system.web>
<globalization fileEncoding="utf-8" culture="auto" uiCulture="auto" enableClientBasedCulture="true"/>
<compilation debug="true" targetFramework="4.7.2">
<assemblies>
<add assembly="System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
</assemblies>
</compilation>
<httpRuntime targetFramework="4.7.2"/>
切换回 4.7.1 使 intellisense 给出了添加 using System.Net.Http;
而不是 using System.Web.Http;
的建议并且似乎修理它。但是为什么?
所以我现在可以发布“答案”:恢复到 4.7.1。所以问题是:为什么这样可以解决问题?
编辑
我已经手动从 packages.config 移动到 packagereference 并找到这个:Visual Studio 2017 - Could not load file or assembly 'System.Runtime, Version=4.1.0.0' or one of its dependencies
据说应该有一个迁移选项。但我没有看到它并删除了 .config 文件。我已经重新安装了大部分 nuget 包,但我仍然停留在 System.Runtime, Version=4.2.1.0
即使我安装了 4.3.0 nuget。通常 VS 在调试信息中给我一个选项,用于双击和重定向 web.config 中的程序集绑定(bind)。但它不存在,所以也许是手动添加?
我也试过添加
<ItemGroup>
<PackageReference Include="Legacy2CPSWorkaround" Version="1.0.0">
<PrivateAssets>All</PrivateAssets>
</PackageReference>
</ItemGroup>
所有其他 nuget 的包引用。但这也无济于事。我还尝试添加 NetStandard.Library nuget,但这也无济于事......
添加
<dependentAssembly>
<assemblyIdentity name="System.Runtime" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.1.1.0" newVersion="4.3.0" />
</dependentAssembly>
这也没有帮助。之前web.config中没有system.runtime。它仍在尝试查找 System.Runtime, Version=4.2.1.0
。
最佳答案
.NET 4.7.2 不需要任何包或 bindingRedirects。简单地引用 System.Net.Http 和“使用 System.Net.Http”应该使 HttpResponseMessage 可用。
这个问题是相关的:https://github.com/dotnet/sdk/issues/2221
请安装 Visual Studio 2017 更新 15.8。它将确保项目文件和依赖项适用于您的方案。
参见:https://blogs.msdn.microsoft.com/visualstudio/2018/08/14/visual-studio-2017-version-15-8/
关于c# - 找不到类型或命名空间名称 'HttpResponseMessage',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51724229/