HttpResponseMessage在Web Api(.NET 4.5)中不起作用 [英] HttpResponseMessage not working in Web Api (.NET 4.5)
问题描述
我阅读了 http://www.asp.net/web-api/overview/security/working-with-ssl-in-web-api ,并尝试使用该页面中的代码:
I read http://www.asp.net/web-api/overview/security/working-with-ssl-in-web-api and tried to use the code from that page:
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);
}
}
}
构建时没有出现错误,但是运行时出现此错误:
When I build I don't get an error, but runtime gives this error:
CS0012:类型'System.Net.Http.HttpRequestMessage'是在未引用的程序集中定义的.您必须添加对程序集'System.Net.Http,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b03f5f7f11d50a3a'的引用.
CS0012: The type 'System.Net.Http.HttpRequestMessage' is defined in an assembly that is not referenced. You must add a reference to assembly '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.
I have got a reference to System.Net.Http in my references folder of my project. If I look at it's properties, it says Version 4.0.0.0 and Runtime Version 4.0.30319. My project properties says target framework is .NET 4.5.
我在VS2013 Express中的IntelliSense也不想获取与HttpResponseMessage或HttpRequestMessage有关的任何内容.
My IntelliSense in VS2013 Express also doesn't want to pick up anything to do with HttpResponseMessage or HttpRequestMessage.
我尝试删除引用并重新添加它,但无济于事.
I've tried removing the reference and re-adding it, but to no avail.
任何帮助将不胜感激.
推荐答案
好吧,我可能会迟到,但以防万一有人遇到这个问题.
Well, I might be late, but just in case someone else faced this problem.
首先,您需要找到一个字符串:
First of all, you need to find a string:
<compilation debug="true" targetFramework="4.5"/>
并使其不像这样自动关闭标签:
And make it not self-closing tag like that:
<compilation debug="true" targetFramework="4.5">
</compilation>
接下来,在其中添加带有您之前所豪宅信息的Assemblys标签,如下所示:
Next, add assemblies tag inside with assembly information you mansioned before, so it looks like this:
<compilation debug="true" targetFramework="4.5">
<assemblies>
<add assembly="System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
</assemblies>
</compilation>
And rebuild your solution. Take a look at this link.
这篇关于HttpResponseMessage在Web Api(.NET 4.5)中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!