如何将Expires响应头添加到Web API Action响应中?

时间:2013-03-06 19:57:21

标签: caching http-headers asp.net-web-api

我很确定“Expires”是有效的HTTP Response Header类型。但是当我尝试在我的代码中设置它时:(这是在ActionFilter.OnActionExecuted方法中)

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

我最终得到了一个例外:

  

InvalidOperationException:未使用的标题名称。确保请求   标头与HttpRequestMessage一起使用,响应头与   HttpResponseMessage和带有HttpContent对象的内容标题。

2 个答案:

答案 0 :(得分:24)

Expires是一个内容标题。试试这个:

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

答案 1 :(得分:1)

尝试

response.Content.Headers.Expires = DateTimeOffset.Now.AddDays(7);
相关问题