WebApi 2.2 IIS 7.5正在设置max-age

时间:2015-04-06 12:18:22

标签: asp.net-web-api iis-7.5

我有一个.Net 4.5.2 WebApi 2.2 REST服务。 Windows 7机器运行IIS 7.5。它不会做太多但返回当前日期/时间。在IIS中托管时,我得到如下所示的响应标头:

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"}

我的自定义标题(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

当我在代码中更改缓存控制标头时,我看到自托管版本中的更改,但有些东西会将IIS版本中的标头覆盖回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?

谢谢,

肖恩

1 个答案:

答案 0 :(得分:0)

所以,我应该在发布问题之前等待一个小时....结果问题是CacheOutputAttribute(OutputCache.V2)不仅设置在特定资源上,而且已添加到全局过滤器集合中。由于它继承自FilterAttribute,IActionFilter,因此它可以作为全局过滤器使用。这就是所有呼叫都被缓存的原因。为了找到问题,我们把项目简化为无用。

肖恩