Delete server header in WebApi native response

We use self-service WebApi, and we need to remove the server header (Server: Microsoft-HTTPAPI / 2.0) of the sent responses.

Since it is hosted, the HttpModule is not an option. Implementation DelegatingHandler, access to headers is possible, as well as addition. asp.net describes how this can be done.

But the server header seems to be added much later in the pipeline, as it is not set to HttpResponseMessagewhich we are returning from DelegatingHandler. However, we can add values.

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

Both Server.Addand .Addare working properly. response.Headers.Remove("Server");however, it does not work, because the server header is not set, response.Headers.Serverempty.

Is there something I don't see?

+4

Source: https://habr.com/ru/post/1546541/


All Articles