17

応答に「max-age」ヘッダーを追加しようとしています。私のVisual Studio開発サーバーでは問題なく動作しますが、アプリをIISに移動するとすぐに(ローカルでIISエクスプレスとサーバーでIISの両方を試しました)、ヘッダーが消えます。

私のコード:

Response.Cache.SetCacheability(HttpCacheability.Public);
Response.Cache.SetMaxAge(new TimeSpan(1, 0, 0, 0));

VS Dev サーバーの応答 (すべて正常に動作します):

HTTP/1.1 200 OK
Server: ASP.NET Development Server/10.0.0.0
Date: Fri, 07 Jan 2011 14:55:04 GMT
X-AspNet-Version: 2.0.50727
Cache-Control: public, max-age=86400

IIS7 の応答

HTTP/1.1 200 OK
Server: Microsoft-IIS/7.5
Date: Fri, 07 Jan 2011 15:00:54 GMT
X-AspNet-Version: 2.0.50727
Cache-Control: public

PS。それが重要な場合、それはASHXハンドラーです...

4

3 に答える 3

31

更新: 2011-03-14 SetSlidingExpiration(true) を確実に呼び出すように修正

context.Response.Cache.SetCacheability(HttpCacheability.Public);
context.Response.Cache.SetMaxAge(TimeSpan.FromMinutes(5));
context.Response.ContentType = "image/jpeg";
context.Response.Cache.SetSlidingExpiration(true);

OutputCache モジュールを削除すると、目的の結果が得られます。これはバグだと思います。

したがって、web.config で次のようにします。

  <system.webServer>
      <modules runAllManagedModulesForAllRequests="true">
          <remove name="OutputCache"/>
      </modules>
  </system.webServer>

追加: 追加情報があります。

  1. MVC の OutputCacheAttribute を使用しても、明らかにこの問題はありません
  2. 同じ MVC アプリケーションの下で、モジュールから「OutputCache」を削除せずに、IHttpHandler または ActionResult の場合の直接実装により、s-maxage が削除されます

次はs-maxageを取り除きます

         public void ProcessRequest(HttpContext context)
    {
        using (var image = ImageUtil.RenderImage("called from IHttpHandler direct", 5, DateTime.Now))
        {
            context.Response.Cache.SetCacheability(HttpCacheability.Public);
            context.Response.Cache.SetMaxAge(TimeSpan.FromMinutes(5));
            context.Response.ContentType = "image/jpeg";
            image.Save(context.Response.OutputStream, ImageFormat.Jpeg);
        }            
    }

次はs-maxageを取り除きます

          public ActionResult Image2()
    {
        MemoryStream oStream = new MemoryStream();

        using (Bitmap obmp = ImageUtil.RenderImage("Respone.Cache.Setxx calls", 5, DateTime.Now))
        {
            obmp.Save(oStream, ImageFormat.Jpeg);
            oStream.Position = 0;
            Response.Cache.SetCacheability(HttpCacheability.Public);
            Response.Cache.SetMaxAge(TimeSpan.FromMinutes(5));
            return new FileStreamResult(oStream, "image/jpeg");
        }
    }

これはそうではありません-理解してください...

    [OutputCache(Location = OutputCacheLocation.Any, Duration = 300)]
    public ActionResult Image1()
    {
        MemoryStream oStream = new MemoryStream();

        using (Bitmap obmp = ImageUtil.RenderImage("called with OutputCacheAttribute", 5, DateTime.Now))
        {
            obmp.Save(oStream, ImageFormat.Jpeg);
            oStream.Position = 0;
            return new FileStreamResult(oStream, "image/jpeg");
        }
    }
于 2011-03-11T17:31:19.167 に答える
2

解決:

web.config:

    <staticContent>
      <clientCache cacheControlCustom="public" cacheControlMode="UseMaxAge" cacheControlMaxAge="1.00:00:00"/>
    </staticContent>

およびIIS PCで:

cmd で に移動しc:\windows\system32\inetsrvます。

次に実行します。

appcmd unlock config /section:staticContent
于 2011-10-01T10:15:23.577 に答える