我试图在Web api响应中设置Expires标头值,但是即使将其设置为7天后失效,它也无法正常工作。
respMsg.Content.Headers.Expires = DateTimeOffset.Now.AddDays(7);
我将那行代码添加到了HttpResponseMessage中,最后将其返回。在响应标头中,我的Expires字段仍为-1。
下面是我的GET方法代码:
public HttpResponseMessage Get()
{
List<string> vals = new List<string>();
using (SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandType = CommandType.Text;
cmd.CommandText = @"
SELECT [ID]
,[Name]
,[Age]
FROM [Customers]
";
cmd.Connection = conn;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
foreach(DataRow dr in dt.Rows)
{
vals.Add(dr["Name"].ToString());
}
}
}
}
}
var respMsg = Request.CreateResponse(HttpStatusCode.OK, vals);
respMsg.Content.Headers.Expires = DateTimeOffset.Now.AddDays(7);
return respMsg;
}