3
Answers

Check my Code

Majid Kamali

Majid Kamali

13y
2.8k
1
Hi.

I have a website that has many png files. so I want to cache them.

please check correctness of my code and mention its problems.
Thanks

// Class1
public class Class1 : IHttpHandler
  {
  public bool IsReusable
  {
  get { return true; }
  }

  public void ProcessRequest(HttpContext context)
  {
  string file = context.Server.MapPath(context.Request.FilePath);
  string filename = file.Substring(file.LastIndexOf('\\') + 1);

  context.Response.Cache.SetExpires(DateTime.Now.Add(new TimeSpan(0, 1, 0)));  // SET EXPIRATION TO 1 MINUTE, (THIS IS AN EXAMPLE)
  context.Response.Cache.SetCacheability(HttpCacheability.Public);
  context.Response.Cache.SetValidUntilExpires(false);
  context.Response.ContentType = "image/png";

  context.Response.AddHeader("content-disposition", "inline; filename=" + filename);
  context.Response.WriteFile(file);
  }

//Web.config
 <add verb="*" path="*.png" type="Nokhbegan_New.Class1"/>

Answers (3)