Cache-Control headers 尽管已在响应中配置,但未在响应中发送 object
Cache-Control headers not sent in response despite being configured on response object
我正在尝试在 ASP.NET MVC Web API 中设置缓存 headers,但 IIS 的响应表明 CacheControl 值集被忽略了。
我最初的假设是我在 System.Web.Http.Cors 中使用了 EnableCorsAttribute,这在这个用例中是必需的。但是,即使没有该属性,响应 Cache-Control header 仍然是 'private'.
我这里有什么地方做错了吗?
// GET api/<version>/content
// [EnableCors(origins: "*", headers: "*", methods: "*")]
public HttpResponseMessage Get(HttpRequestMessage request)
{
int cacheMaxAgeSeconds;
string cacheMaxAgeString = request.GetQueryString("cache-max-age") ?? request.GetQueryString("cache-max-age-seconds");
string rawUri = request.RequestUri.ToString();
try
{
cacheMaxAgeSeconds = cacheMaxAgeString == null ? Config.ApiCacheControlMaxSeconds : int.Parse(cacheMaxAgeString);
}
catch (Exception ex)
{
cacheMaxAgeSeconds = Config.ApiCacheControlMaxSeconds;
//...
}
try
{
//...
var response = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent("...", Encoding.UTF8, "application/json")
};
response.Headers.CacheControl = new CacheControlHeaderValue
{
Public = true,
MaxAge = TimeSpan.FromSeconds(cacheMaxAgeSeconds)
};
return response;
}
catch (Exception apiEx)
{
//...
}
}
回应
HTTP/1.1 200 OK
Cache-Control: private
Content-Type: application/json; charset=utf-8
Date: Thu, 23 Jul 2015 10:53:17 GMT
Server: Microsoft-IIS/7.5
Set-Cookie: ASP.NET_SessionId=knjh4pncbrhad30kjykvwxyz; path=/; HttpOnly
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Content-Length: 2367
Connection: keep-alive
下面的代码在 vanilla WebApi 应用程序 (System.Web.Http 4.0.0.0) 中正确设置 "cache-control: public, max-age=15"。所以...导致问题的可能不是 WebApi 本身。
您的项目中可能有一些更改缓存设置的魔法(想想全局操作过滤器或类似的东西)。或者您可能正在通过重写 HTTP headers 的代理。
public HttpResponseMessage Get()
{
var content = new JavaScriptSerializer().Serialize(new { foo = "bar" });
var response = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent(content, Encoding.UTF8, "application/json")
};
response.Headers.CacheControl = new CacheControlHeaderValue
{
Public = true,
MaxAge = TimeSpan.FromSeconds(15)
};
return response;
}
// returns in the response: "Cache-Control: public, max-age=15"
几周后得到的答案:
Cache-Control header 在 运行ning debug 构建时似乎设置为 'private'。当我 运行 使用发布版本时,问题就消失了。
要添加可能导致此问题的另一件事:
您 运行 通过 Owin 管道。
在这种情况下,您需要在如下定义的 Owin 中间件中设置 headers:
class MiddleWare : OwinMiddleware
{
public MiddleWare(OwinMiddleware next)
: base(next)
{
}
public override async Task Invoke(IOwinContext context)
{
context.Response.Headers["Cache-Control"] = "no-cache, no-store, must-revalidate";
context.Response.Headers["Pragma"] = "no-cache";
context.Response.Headers["Expires"] = "0";
await Next.Invoke(context);
}
}
我正在尝试在 ASP.NET MVC Web API 中设置缓存 headers,但 IIS 的响应表明 CacheControl 值集被忽略了。
我最初的假设是我在 System.Web.Http.Cors 中使用了 EnableCorsAttribute,这在这个用例中是必需的。但是,即使没有该属性,响应 Cache-Control header 仍然是 'private'.
我这里有什么地方做错了吗?
// GET api/<version>/content
// [EnableCors(origins: "*", headers: "*", methods: "*")]
public HttpResponseMessage Get(HttpRequestMessage request)
{
int cacheMaxAgeSeconds;
string cacheMaxAgeString = request.GetQueryString("cache-max-age") ?? request.GetQueryString("cache-max-age-seconds");
string rawUri = request.RequestUri.ToString();
try
{
cacheMaxAgeSeconds = cacheMaxAgeString == null ? Config.ApiCacheControlMaxSeconds : int.Parse(cacheMaxAgeString);
}
catch (Exception ex)
{
cacheMaxAgeSeconds = Config.ApiCacheControlMaxSeconds;
//...
}
try
{
//...
var response = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent("...", Encoding.UTF8, "application/json")
};
response.Headers.CacheControl = new CacheControlHeaderValue
{
Public = true,
MaxAge = TimeSpan.FromSeconds(cacheMaxAgeSeconds)
};
return response;
}
catch (Exception apiEx)
{
//...
}
}
回应
HTTP/1.1 200 OK
Cache-Control: private
Content-Type: application/json; charset=utf-8
Date: Thu, 23 Jul 2015 10:53:17 GMT
Server: Microsoft-IIS/7.5
Set-Cookie: ASP.NET_SessionId=knjh4pncbrhad30kjykvwxyz; path=/; HttpOnly
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Content-Length: 2367
Connection: keep-alive
下面的代码在 vanilla WebApi 应用程序 (System.Web.Http 4.0.0.0) 中正确设置 "cache-control: public, max-age=15"。所以...导致问题的可能不是 WebApi 本身。
您的项目中可能有一些更改缓存设置的魔法(想想全局操作过滤器或类似的东西)。或者您可能正在通过重写 HTTP headers 的代理。
public HttpResponseMessage Get()
{
var content = new JavaScriptSerializer().Serialize(new { foo = "bar" });
var response = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent(content, Encoding.UTF8, "application/json")
};
response.Headers.CacheControl = new CacheControlHeaderValue
{
Public = true,
MaxAge = TimeSpan.FromSeconds(15)
};
return response;
}
// returns in the response: "Cache-Control: public, max-age=15"
几周后得到的答案:
Cache-Control header 在 运行ning debug 构建时似乎设置为 'private'。当我 运行 使用发布版本时,问题就消失了。
要添加可能导致此问题的另一件事:
您 运行 通过 Owin 管道。
在这种情况下,您需要在如下定义的 Owin 中间件中设置 headers:
class MiddleWare : OwinMiddleware
{
public MiddleWare(OwinMiddleware next)
: base(next)
{
}
public override async Task Invoke(IOwinContext context)
{
context.Response.Headers["Cache-Control"] = "no-cache, no-store, must-revalidate";
context.Response.Headers["Pragma"] = "no-cache";
context.Response.Headers["Expires"] = "0";
await Next.Invoke(context);
}
}