Eric Falsken
Eric Falsken

Reputation: 4934

How to add an Expires response header to a WebAPI Action response?

I'm pretty sure that "Expires" is valid HTTP Response Header type. But when I try to set it in my code: (this is in an ActionFilter.OnActionExecuted method)

actionExecutedContext.Response.Headers.Add("Expires", (DateTime.Now + Timespan.FromDays(7)).ToString("R"));

I end up with an exception:

InvalidOperationException: Misused header name. Make sure request headers are used with HttpRequestMessage, response headers with HttpResponseMessage, and content headers with HttpContent objects.

Upvotes: 15

Views: 6778

Answers (2)

Youssef Moussaoui
Youssef Moussaoui

Reputation: 12395

Expires is a content header. Try this instead:

actionExecutedContext.Response.Content.Headers.Expires = DateTimeOffset.Now.AddDays(7);

Upvotes: 27

RaghuRam Nadiminti
RaghuRam Nadiminti

Reputation: 6793

Try

response.Content.Headers.Expires = DateTimeOffset.Now.AddDays(7);

Upvotes: 1

Related Questions