HttpResponseMessage无法在Web Api(.NET 4.5)中工作
我阅读http://www.asp.net/web-api/overview/security/working-with-ssl-in-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 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。 我的项目属性说目标框架是.NET 4.5。
我在VS2013 Express中的IntelliSense也不想接受与HttpResponseMessage或HttpRequestMessage有关的任何事情。
我已经尝试删除引用并重新添加它,但无济于事。
任何帮助将非常感激。
在你的web.config中尝试添加:
好吧,我可能会迟到,但以防其他人遇到这个问题。
首先,您需要找到一个字符串:
并使它不是那样的自动关闭标签:
接下来,使用您之前管理的assembly信息在里面添加assembly标签,所以它看起来像这样:
并重建您的解决方案。 看看这个链接 。
尝试在配置文件中添加程序集绑定
我尝试了这个高度建议的解决方案(我发现这是SO中许多类似问题的公认答案)
在
Web.Config
编写并重建解决方案
但不幸的是,我没有运气。 最后,我找到了一个适合我的解决方案并节省了我的一天:)
- 右键单击“
References
文件夹>“Add Reference...
- 展开窗口左侧的Assemblies,然后选择Framework。
- 滚动并选择程序集列表中的
System.Net.Http
。 确保选中System.Net.Http
旁边的框,然后单击“OK
。 - 重建项目。
有同样的问题,我刚刚添加了’System.Net.Http’Nuget Package并且它有效。
- unit testing自定义ConfigurationElement和ConfigurationElementCollection
- ClientScript.RegisterStartupScript()
- 为Web Api版本控制自定义MapHttpAttributeRoutes
- 有没有为WebAPI项目生成C#HTTPClient包装器的方法?
- 在Web API中未发布数据时,请避免使用null模型
- ASP.net MVC 4 WebApi – 测试MIME多部分内容
- config.MapODataServiceRoute错误
- 安装CORS后无法加载文件
- ASP.NET Web API – 没有“MediaTypeFormatter”可用于读取“Int32”类型的对象
- 设置JsonConvert.DefaultSettings asp net core 2.0不能按预期工作
- WebApi用于Get和Post的不同DTO