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

耗尽温柔 提交于 2019-12-12 10:29:10

问题


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.


回答1:


Expires is a content header. Try this instead:

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



回答2:


Try

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


来源:https://stackoverflow.com/questions/15256785/how-to-add-an-expires-response-header-to-a-webapi-action-response

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!