How to use output caching on .ashx handler

后端 未结 5 1851
遥遥无期
遥遥无期 2020-12-02 13:40

How can I use output caching with a .ashx handler? In this case I\'m doing some heavy image processing and would like the handler to be cached for a minute or so.

Al

5条回答
  •  心在旅途
    2020-12-02 14:10

    I used the following with success and thought it worthwhile to post here .

    Manually controlling the ASP.NET page output cache

    From http://dotnetperls.com/cache-examples-aspnet

    Setting cache options in Handler.ashx files

    First, you can use HTTP handlers in ASP.NET for a faster way to server dynamic content than Web Form pages. Handler.ashx is the default name for an ASP.NET generic handler. You need to use the HttpContext parameter and access the Response that way.

    Sample code excerpted:

    <%@ WebHandler Language="C#" Class="Handler" %>
    

    C# to cache response for 1 hour

    using System;
    using System.Web;
    
    public class Handler : IHttpHandler {
    
        public void ProcessRequest (HttpContext context) {
            // Cache this handler response for 1 hour.
            HttpCachePolicy c = context.Response.Cache;
            c.SetCacheability(HttpCacheability.Public);
            c.SetMaxAge(new TimeSpan(1, 0, 0));
        }
    
        public bool IsReusable {
            get {
                return false;
            }
        }
    }
    

提交回复
热议问题