C# HttpResponseMessage在Web Api(.NET 4.5)中不起作用
我阅读并尝试使用该页面中的代码:C# HttpResponseMessage在Web Api(.NET 4.5)中不起作用,c#,asp.net-web-api,C#,Asp.net Web Api,我阅读并尝试使用该页面中的代码: using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http; using System.Web; using System.Web.Http; using System.Web.Http.Filters; using System.Web.Http.Controllers; public class Requ
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using System.Web.Http.Filters;
using System.Web.Http.Controllers;
public class RequireHttpsAttribute : AuthorizationFilterAttribute
{
public override void OnAuthorization(HttpActionContext actionContext)
{
if (actionContext.Request.RequestUri.Scheme != Uri.UriSchemeHttps)
{
actionContext.Response = new HttpResponseMessage(System.Net.HttpStatusCode.Forbidden)
{
ReasonPhrase = "HTTPS Required"
};
}
else
{
base.OnAuthorization(actionContext);
}
}
}
当我构建时,我没有得到错误,但运行时会给出以下错误:
CS0012:在未引用的程序集中定义了类型“System.Net.Http.HttpRequestMessage”。您必须添加对程序集“System.Net.Http,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a”的引用
我在我的项目的references文件夹中获得了对System.Net.Http的引用。如果我查看它的属性,它会显示版本4.0.0.0和运行时版本4.0.30319。我的项目属性说目标框架是.NET4.5
我在VS2013 Express中的IntelliSense也不想获取任何与HttpResponseMessage或HttpRequestMessage有关的信息
我尝试删除引用并重新添加,但没有效果
非常感谢您的帮助。在您的web.config中尝试添加:
<configuration>
<system.web>
<compilation>
<assemblies>
<add assembly="System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
</assemblies>
</compilation>
</system.web>
</configuration>
尝试在配置文件中添加程序集绑定
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" />
<bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime
嗯,我可能会迟到,但以防万一别人会遇到这个问题
首先,您需要找到一个字符串:
<compilation debug="true" targetFramework="4.5"/>
并使其不是像这样的自动关闭标签:
<compilation debug="true" targetFramework="4.5">
</compilation>
接下来,将Assemblys标记添加到包含您之前指定的程序集信息的内部,如下所示:
<compilation debug="true" targetFramework="4.5">
<assemblies>
<add assembly="System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
</assemblies>
</compilation>
.我曾尝试过这一高度建议的解决方案(我发现这是SO中许多类似问题的公认答案)
在Web.Config
中编写此代码并重新生成解决方案
<system.web>
<compilation debug="true" targetFramework="4.0" />
<compilation debug="true" targetFramework="4.0">
<assemblies>
<add assembly="System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
</assemblies>
</compilation>
</system.web>
但不幸的是,我在这方面运气不佳。最后,我找到了一个对我有效的解决方案,挽救了我的一天:)
右键单击引用
文件夹>添加引用…
展开窗口左侧的“部件”,然后选择“框架”
滚动到并在程序集列表中选择System.Net.Http
。
确保选中了System.Net.Http
旁边的框,然后单击
正常
李>
重建项目
遇到同样的问题,我刚刚重新安装了'System.Net.Http'Nuget软件包,它运行正常。但不幸的是,我在这方面运气不佳。最后,我找到了一个对我有效的解决方案,挽救了我的一天:)
右键单击引用文件夹>添加引用。。。
展开窗口左侧的“部件”,然后选择“框架”。
滚动到并在程序集列表中选择System.Net.Http。确保选中System.Net.Http旁边的框,然后单击“确定”。
重建项目
这个很好用。Go Head针对at的问题,我采用了上述所有解决方案:
actionContext.Response = new HttpResponseMessage(statusCode: HttpStatusCode.UpgradeRequired);
其中.Response和HttpResponseMessage&HttpStatusCode将以
CS0012:类型“System.Net.Http.HttpRequestMessage”是在
未引用的程序集。必须添加对程序集的引用
'System.Net.Http,版本=4.0.0.0,区域性=中性,
PublicKeyToken=b03f5f7f11d50a3a'
我删除并重新添加了对System.Net.Http的引用,在web.config文件中添加了该条目,还使用以下命令从Nuget package manager安装了该条目:
PM> Install-Package System.Net.Http
但它并没有起作用。最后,我关闭并打开了解决方案,上面的所有修复都在中,然后它开始工作。不要忘记system.web
和assemblies
之间的编译
元素。这对我来说很有帮助。它编译时没有问题,但intelliSense在razor视图引擎中显示为错误,直到我重新启动Visual Studio。这是“Visual Studio Mac”的解决方案。