【问题标题】:How can I get a full control over the HTTP headers in ASP.Net WebAPI 2?如何完全控制 ASP.Net WebAPI 2 中的 HTTP 标头?
【发布时间】:2014-04-24 15:57:40
【问题描述】:

使用 HttpResponseMessage 作为 Get 操作的返回类型的默认行为如下所示:

HTTP/1.1 200 OK
Content-Length: 10
Content-Type: text/plain; charset=utf-16
Server: Microsoft-IIS/8.0
Date: Mon, 27 Jan 2014 08:53:35 GMT

<my data>

我想要的是完全控制响应中的 http 标头。

【问题讨论】:

  • 您可以在 Web.Config 中使用httpProtocol:customHeaders 删除一些...是否有特殊原因要控制所有标题(因为可能有比自己编写更好的解决方案)...跨度>
  • 我想为瘦客户端(基于微控制器的设备)提供 HTTP 服务(非 REST)。

标签: c# asp.net asp.net-web-api


【解决方案1】:

ApiController 类中的默认 Get 方法:

    [HttpGet]
    public HttpResponseMessage Ping()
    {
        HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK);
        response.Headers.Add("X-Custom-Header", "This is my custom header.");
        response.Headers.Remove("Server");

        return response;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-14
    • 1970-01-01
    • 2013-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-27
    相关资源
    最近更新 更多