Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 679
Eric Falsken Avatar asked Mar 06 '13 19:03

Eric Falsken


People also ask

What is expired response header?

The Expires HTTP header contains the date/time after which the response is considered expired. Invalid expiration dates with value 0 represent a date in the past and mean that the resource is already expired.

Can we set response header?

Select the web site where you want to add the custom HTTP response header. In the web site pane, double-click HTTP Response Headers in the IIS section. In the actions pane, select Add. In the Name box, type the custom HTTP header name.


2 Answers

Expires is a content header. Try this instead:

actionExecutedContext.Response.Content.Headers.Expires = DateTimeOffset.Now.AddDays(7);
like image 125
Youssef Moussaoui Avatar answered Oct 10 '22 00:10

Youssef Moussaoui


Try

response.Content.Headers.Expires = DateTimeOffset.Now.AddDays(7);
like image 45
RaghuRam Nadiminti Avatar answered Oct 10 '22 02:10

RaghuRam Nadiminti