15
votes

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.

2

2 Answers

25
votes

Expires is a content header. Try this instead:

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

Try

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