首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >缓存ASHX图像响应

缓存ASHX图像响应
EN

Stack Overflow用户
提问于 2011-09-26 19:23:48
回答 1查看 6.1K关注 0票数 4

我创建了一个ashx文件来动态生成图像缩略图。我想缓存这些图像客户端后,他们被称为第一次。

URL:

~/image.ashx?dir=user&w=25&h=25&force=yes&img=matt.jpg

代码背后:

代码语言:javascript
复制
public void ProcessRequest (HttpContext context) {
    TimeSpan refresh = new TimeSpan(0, 15, 0);
    context.Response.Cache.SetExpires(DateTime.Now.Add(refresh));
    context.Response.Cache.SetMaxAge(refresh);
    context.Response.Cache.SetCacheability(HttpCacheability.Server);
    context.Response.CacheControl = HttpCacheability.Public.ToString();
    context.Response.Cache.SetValidUntilExpires(true);

    string dir = context.Request.QueryString["dir"];
    string img = context.Request.QueryString["img"];
    bool force = context.Request.QueryString["force"] == "yes";
    double w = 0;
    double h = 0;
    ...
    context.Response.ContentType = "image/jpeg";
    thumb.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}

使用Chrome中的Dev工具,我可以看到以下响应头:

代码语言:javascript
复制
HTTP/1.1 200 OK
Server: ASP.NET Development Server/10.0.0.0
Date: Mon, 26 Sep 2011 19:17:31 GMT
X-AspNet-Version: 4.0.30319
Set-Cookie: .ASPXAUTH=...; path=/; HttpOnly
Cache-Control: no-cache
Pragma: no-cache
Expires: -1
Content-Type: image/jpeg
Content-Length: 902
Connection: Close

有人能告诉我为什么不使用相同的URL对多个调用进行缓存吗?任何提示都将不胜感激。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-09-26 19:26:59

当然,这句话:

代码语言:javascript
复制
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.Server);

应:

代码语言:javascript
复制
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.Public);
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7560180

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档