我想使用自定义 DelegatingHandler 将 E-Tag 标头添加到我的一些 Web API 调用的响应中。这是我SendAsync()
覆盖的一部分:
if (request.Method == HttpMethod.Get)
{
HttpResponseMessage response;
if (request.Headers.IfNoneMatch.Count == 0)
{
response = await base.SendAsync(request, cancellationToken);
response.Headers.Add("X-MyStupidTestHeader","It's here"); //<- this is always on my responses
response.Content.Headers.Expires = DateTimeOffset.Now.AddMinutes(10.0);
var tag = TagStore.Fetch(request.RequestUri); // <- this is non-null
if (tag != null)
response.Headers.ETag = new EntityTagHeaderValue(tag); //<- this is nowhere to be found. WTF?
return response;
}
...
}
当我在 Fiddler 监听的浏览器中调用该 url 时,我得到以下响应:
HTTP/1.1 200 OK
Server: ASP.NET Development Server/11.0.0.0
Date: Wed, 10 Jul 2013 16:08:00 GMT
X-AspNet-Version: 4.0.30319
X-MyStupidTestHeader: It's here
Cache-Control: no-cache
Pragma: no-cache
Expires: -1
Content-Type: text/html; charset=utf-8
Content-Length: 18
Connection: Close
{
"carId": 2
}
我知道我的 DelegatingHandler 被调用了,因为我已经设置了断点。在我的SendAsync
覆盖之外,响应有一个 E-Tag 值;但是当它从服务器出来时,它就不在那里了,所有这些缓存头都在那里,告诉客户端不要缓存。我所有的谷歌搜索都表明由我来设置缓存,这很好,但是如何阻止 Web API不缓存?
更新:我意识到有这类东西的库,但我宁愿不使用它们。