gpt4 book ai didi

c# - HttpResponseMessage 在 Web Api (.NET 4.5) 中不起作用

转载 作者:太空狗 更新时间:2023-10-29 17:34:11 26 4
gpt4 key购买 nike

我读了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: 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'.

我在项目的引用文件夹中获得了对 System.Net.Http 的引用。如果我查看它的属性,它会显示版本 4.0.0.0 和运行时版本 4.0.30319。我的项目属性显示目标框架是 .NET 4.5。

我在 VS2013 Express 中的 IntelliSense 也不想获取与 HttpResponseMessage 或 HttpRequestMessage 有关的任何信息。

我试过删除引用并重新添加它,但无济于事。

如有任何帮助,我们将不胜感激。

最佳答案

好吧,我可能会迟到,但以防其他人遇到这个问题。

首先,你需要找到一个字符串:

<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>

并重建您的解决方案。看看this link .

关于c# - HttpResponseMessage 在 Web Api (.NET 4.5) 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20220344/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com