Can't set Content-Type header on HttpResponseMessage headers?
Have a look at the HttpContentHeaders.ContentType Property:
response.Content.Headers.ContentType = new MediaTypeHeaderValue("text/plain");
if (response.Content == null){ response.Content = new StringContent(""); // The media type for the StringContent created defaults to text/plain.}
Something is missing in ASP Web API: the EmptyContent
type. It will allow sending an empty body while still allowing all content-specific headers.
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 as you wish. You now have a content object for your extra 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 lots of codes (because it inheritsByteArrayContent
)- so let's save a few nanoseconds
StringContent
will add an extra useless/problematic header:Content-Type: plain/text; charset=...
- so let's save a few network bytes