我们正在使用自托管的 WebApi,我们需要删除所发送响应的服务器标头(服务器:Microsoft-HTTPAPI/2.0)。
由于它是自托管的,因此 HttpModule 不是一种选择。实现 a DelegatingHandler,访问标头以及添加是可能的。在asp.net网站很好的细节,如何能够做到这一点。
但是服务器标头似乎是在管道中稍后添加的,因为它没有在HttpResponseMessage我们从DelegatingHandler. 但是,我们可以添加值。
async protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
HttpResponseMessage response = await base.SendAsync(request, cancellationToken);
response.Headers.Server.Add(new ProductInfoHeaderValue("TestProduct", "1.0"));
response.Headers.Add("Server", "TestServerHeader");
return response;
}
Run Code Online (Sandbox Code Playgroud)
双方Server.Add并.Add正常工作。response.Headers.Remove("Server");但是不起作用,因为服务器标头未设置,response.Headers.Server为空。
有什么我想念的吗?