ASP.NET中与HTTP缓存相关的标头的有效含义 [英] Effective meaning of HTTP cache-related headers in ASP.NET

查看:96
本文介绍了ASP.NET中与HTTP缓存相关的标头的有效含义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ASP.NET 2.0中的Web应用程序,该应用程序涉及通过资源处理程序(.ashx)提供图像。我刚刚实现了处理缓存头和条件GET请求,因此我不必为每个请求提供所有图像。但我不确定我是否完全理解浏览器的缓存正在发生什么。

I'm working on a web app in ASP.NET 2.0 that involves serving images via a resource handler (.ashx). I've just implemented handling cache headers and conditional GET requests, so that I don't have to serve all the images for every request. But I'm not sure I'm completely understanding what's happening with the browser's cache.

图像是通过网址获取的,例如 http:// www .mysite.com / image.ashx?图像标识= 3 。我在处理程序中的代码如下所示:

Images are fetched via urls like http://www.mysite.com/image.ashx?imageID=3. My code in the handler looks something like this:

int imageID = -1;
try
{
  imageID = Int32.Parse(context.Request["imageID"]);
}
catch (Exception) {}

MyImageClass image = DataLayer.GetImage(imageID);
if (image != null)
{
  DateTime requestedDate = DateTime.MinValue;
  if (context.Request.Headers["If-Modified-Since"] != null)
  {
    requestedDate = DateTime.Parse(context.Request.Headers["If-Modified-Since"])
      .ToLocalTime();
  }

  if (requestedDate < image.ModifiedDate)
  {
     context.Response.AddHeader("content-type", image.ContentType);
     context.Response.CacheControl = HttpCacheability.Private.ToString();
     context.Response.Cache.SetLastModified(image.ModifiedDate.ToUniversalTime());
     context.Response.Cache.SetMaxAge(TimeSpan.FromDays(1));
     //write image to output stream
  }
  else
  {
    context.Response.StatusDescription = "Not Modified";
    context.Response.StatusCode = 304;
  }
}

这是第一次响应头的样子请求图片:

This is what the response header looks like the first time an image is requested:

HTTP/1.1 200 OK
Cache-Control: private, max-age=86400
Content-Length: 1048576
Content-Type: image/jpeg
Expires: Sat, 28 Jan 2012 17:17:11 GMT
Last-Modified: Fri, 27 Jan 2012 16:50:27 GMT
Server: Microsoft-IIS/7.5
X-AspNet-Version: 2.0.50727
X-Powered-By: ASP.NET
Date: Fri, 27 Jan 2012 17:17:10 GMT

这是对后续请求的回复:

And this is a response to a subsequent request:

HTTP/1.1 304 Not Modified
Cache-Control: private
Server: Microsoft-IIS/7.5
X-AspNet-Version: 2.0.50727
X-Powered-By: ASP.NET
Date: Fri, 27 Jan 2012 17:17:30 GMT
Connection: close

在Fiddler中观察请求时,我注意到浏览器(Firefox 9)总是发出有条件的GET请求对于冷杉后的形象请求。它得到 304 Not Modified 响应并从缓存中提取图像,这很棒。但是,在标题的最大年龄(或失效日期)过去之后,是否有办法让它始终从缓存中拉出,甚至不询问服务器?我尝试使用带有未来日期的 context.Response.Cache.SetExpires(),浏览器仍然发出有条件的GET请求。

Watching the requests in Fiddler, I'm noticing that the browser (Firefox 9) always makes a conditional GET request for the image after the first request. It gets the 304 Not Modified response and pulls the image from cache, which is great. But isn't there a way to make it always pull from the cache, without even asking the server, until after the header's max-age (or expiry date) is past? I've tried using context.Response.Cache.SetExpires() with a future date, and the browser still makes the conditional GET request.

推荐答案

当您按 F5 或重新加载时,Firefox将始终发送条件请求。

When you press F5 or Reload, Firefox will always send conditional requests.

如果您正常导航到该页面(例如,单击链接或使用地址栏),它将直接进入缓存。

If you navigate to the page normally (eg, clicking a link or using the address bar), it will go straight to the cache.

这篇关于ASP.NET中与HTTP缓存相关的标头的有效含义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆