WebApi 2.2 IIS 7.5 正在设置 max-age

WebApi 2.2 IIS 7.5 something is setting max-age

我有 .Net 4.5.2 WebApi 2.2 REST 服务。 Windows 7 台机器 运行ning IIS 7.5。它没有做太多,但 return 当前 date/time。在 IIS 中托管时,我收到如下响应 header:

HTTP/1.1 200 OK
Cache-Control: max-age=60
Content-Length: 58
Content-Type: application/json; charset=utf-8
ETag: "c664145c-6923-44b6-b3fb-ff7e50259b44"
Server: Microsoft-IIS/7.5
ApplicationDate: Test with date 4/3/2015 3:18:08 PM
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Fri, 03 Apr 2015 19:18:10 GMT

{"messages":[],"result":"All is well 4/3/2015 3:18:07 PM"}

如果我第二次调用它,我会看到:

HTTP/1.1 200 OK
Cache-Control: max-age=60
Content-Length: 58
Content-Type: application/json
ETag: "c664145c-6923-44b6-b3fb-ff7e50259b44"
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Fri, 03 Apr 2015 19:27:25 GMT

{"messages":[],"result":"All is well 4/3/2015 3:18:07 PM"}

我的自定义 header (ApplicationDate) 不见了,时间也没有改变。关键可能是那里的 Cache-Control: max-age=60 。我不知道它来自哪里!

如果我 运行 它 "Self Hosted",相同的代码...我看到这个:

HTTP/1.1 200 OK
Content-Length: 58
Content-Type: application/json; charset=utf-8
Server: Microsoft-HTTPAPI/2.0
ApplicationDate: Test with date 4/3/2015 3:35:31 PM
Date: Fri, 03 Apr 2015 19:35:31 GMT

{"messages":[],"result":"All is well 4/3/2015 3:35:30 PM"}

IIS 管道中的某些内容正在设置 max-age。 (顺便说一句,使用 SoapUI 和 Fiddler 进行测试,没有使事情复杂化的浏览器问题)

我试过禁用 IIS OutputCache 模块。我使用 FailedReqLogFiles:

进行了验证

OUTPUT_CACHE_LOOKUP_END 结果 4 结果CACHING_DISABLED

当我更改代码中的 cache-control header 时,我看到了 Self-Hosted 版本中的更改,但某些内容覆盖了 IIS 版本中的 header回到 max-age=60。

HTTP/1.1 200 OK
Cache-Control: max-age=60
Pragma: no-cache
Content-Length: 58
Content-Type: application/json; charset=utf-8
ETag: "3ce2e8b6-4891-496e-8bb7-087590239d6b"
Server: Microsoft-IIS/7.5
ApplicationDate: Test with date 4/3/2015 3:49:19 PM
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Fri, 03 Apr 2015 19:49:19 GMT

{"messages":[],"result":"All is well 4/3/2015 3:49:19 PM"}

这是控制器:

/// <summary>
/// Get "REST" status
/// </summary>
/// <returns></returns>
[ActionName("GetStatusDate")]
[Route("GetStatusDate")]
public HttpResponseMessage GetStatusDate()
{
    CommonResponse<string> response = new CommonResponse<string>();
    response.Result = "All is well " + DateTime.Now;
    HttpResponseMessage responseMessage = Request.CreateResponse(HttpStatusCode.OK, response);

    responseMessage.Headers.Add("ApplicationDate", "Test with date " + DateTime.Now);

    responseMessage.Headers.Remove("Cache-Control");

    responseMessage.Headers.CacheControl = new CacheControlHeaderValue()
                                           {
                                               MaxAge = TimeSpan.FromSeconds(11),
                                               NoCache = true,
                                               Private = true
                                           };

    responseMessage.Headers.Add("Pragma","no-cache");

    return responseMessage;

}

设置是什么max-age?

谢谢,

肖恩

所以,我应该再等一个小时才能发布问题....原来问题是 CacheOutputAttribute (OutputCache.V2) 不仅在特定资源上设置,而且已添加到全局过滤器collection。由于它继承自 FilterAttribute、IActionFilter,因此它作为全局过滤器工作得很好。这就是所有调用都被缓存的原因。把项目撕成碎片才找到问题。

肖恩