There is something missing in the ASP Web API: type EmptyContent . This will allow you to send an empty body, while at the same time allowing all headers depending on the content.
Put the following class somewhere in your code:
public class EmptyContent : HttpContent { protected override Task SerializeToStreamAsync(Stream stream, TransportContext context) { return Task.CompletedTask; } protected override bool TryComputeLength(out long length) { length = 0L; return true; } }
Then use it however you want. You now have a content object for your additional headers.
response.Content = new EmptyContent(); response.Content.Headers.LastModified = file.DateUpdatedUtc;
Why use EmptyContent instead of new StringContent(string.Empty) ?
StringContent is a heavy class that executes many codes (because it inherits ByteArrayContent )- so let's save a few nanoseconds
StringContent will add an extra useless / problematic header: Content-Type: plain/text; charset=... Content-Type: plain/text; charset=... Content-Type: plain/text; charset=... Content-Type: plain/text; charset=...- so let's save some network bytes
SandRock Jul 12 '19 at 17:44 2019-07-12 17:44
source share