Кэширование ответа изображения ASHX - PullRequest
4 голосов
/ 26 сентября 2011

Я создал файл Ashx для создания миниатюр изображений на лету. Я хотел бы кэшировать эти изображения на стороне клиента после их первого вызова.

URL:

~ / image.ashx? Реж = пользователь & W = 25 & ч = 25 & сила = да и IMG = matt.jpg

Код сзади:

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, я вижу следующий заголовок ответа:

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? Любые советы будут с благодарностью.

1 Ответ

5 голосов
/ 26 сентября 2011

Наверняка эта строка:

HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.Server);

Должно быть:

HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.Public);
...