如何将Expires响应标头添加到WebAPI Action响应? [英] How to add an Expires response header to a WebAPI Action response?

查看:87
本文介绍了如何将Expires响应标头添加到WebAPI Action响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我非常确定 Expires是有效的HTTP响应标头类型。但是,当我尝试在代码中进行设置时:(这在ActionFilter.OnActionExecuted方法中)

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"));

我最终遇到一个例外:


InvalidOperationException:标头名称误用。确保请求
头与HttpRequestMessage一起使用,响应头与
HttpResponseMessage一起使用,内容头与HttpContent对象一起使用。

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


推荐答案

Expires是一个内容标头。改为尝试以下操作:

Expires is a content header. Try this instead:

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

这篇关于如何将Expires响应标头添加到WebAPI Action响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆