Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 238903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:32:34+00:00 2026-05-11T20:32:34+00:00

I have a custom handler that is returning an image to the browser. The

  • 0

I have a custom handler that is returning an image to the browser.

The images are fetched from a database.

For some reason the images are not being cached by the browser, and I was wondering if someone might be able to spot what I am missing from the below code:

HttpContext.Current.Response.BinaryWrite(imageBytes);
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.Public);
Context.Current.Response.Cache.SetAllowResponseInBrowserHistory(true);
if(imgRepGetCache.DateCached.HasValue)
    HttpContext.Current.Response.Cache.SetLastModified(imgRepGetCache.DateCached.Value);
HttpContext.Current.Response.Cache.SetExpires(DateTime.Now.AddDays(2));
HttpContext.Current.Response.ContentType = "image/jpeg";

Or alternatively if I’m completely missing the point somehow and there’s somewhere else I need to look.

Edit: As per request for more info:

  • The URL is always the same
  • I am testing loading the same file via standard IIS pipe and my pipe in the same browser on the same PC. The one that loads through IIS normally is cached, my file isn’t.

Edit 2: After inspecting the HTTP requests/responses on the normal IIS route I think it has something to do with the ETag. The ETag (which I’m new to as of just now) seems to be a sort of checksum for the document. On subsequent requests by a browser the ETag is sent and if the server finds the ETag hasn’t changed then it returns a 304 – Not Modified. All good! But I’m now setting the ETag using:

HttpContext.Current.Response.Cache.SetETag(imgRepGetCache.DateCached.ToString());

But it doesn’t appear in the response. Closer…

Edit 3: I fixed it in the end after taking advantage of Firebug for some HTTP inspecting fun. I posted my solution below.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T20:32:34+00:00Added an answer on May 11, 2026 at 8:32 pm

    OK, I fixed it.

    Here is what I did for anyone else and for my own future reference:

    // Check for repeated request for the same image from a browser
    if (HttpContext.Current.Request.Headers.Get("If-None-Match") == imgRepGetCache.DateCached.Value.ToString())
    {
        // Return 304 - Not Modified
        HttpContext.Current.Response.Status = "304 Not Modified";
    }
    else
    {
        if (imgRepGetCache.DateCached.HasValue)
            HttpContext.Current.Response.Headers.Set("Etag", imgRepGetCache.DateCached.Value.ToString());
        // ... do my other stuff here
    }
    

    Works a charm!

    If anyone spots any potential problems here, let me know so I can update this.

    To pre-empt one obvious one – I can 100% rely on the date string for identifying whether an image is new or not (in my particular scenario).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a custom control that implements IPostBackEventHandler. Some client-side events invoke __doPostBack(controlID, eventArgs).
I have a custom handler I've written that executes a custom action based on
I currently have a custom session handler class which simply builds on php's session
I have custom errors configured in my web.config, but IIS 6.0 is returning the
I have a few models that need to have custom find conditions placed on
I have a custom installer action that updates the PATH environment, and creates an
I have a multi-browser page that shows vertical text. As an ugly hack to
I need to implement a custom handler for MVC that gives me the first
I have an application that uses a service to create an ArrayList of custom
I have a list that gets refreshed every 2 seconds via the Handler postDelayed()

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.