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

  • SEARCH
  • Home
  • 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 815525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T01:41:55+00:00 2026-05-15T01:41:55+00:00

I have a script, which by using several querystring variables provides an image. I

  • 0

I have a script, which by using several querystring variables provides an image. I am also using URL rewriting within IIS 7.5.

So images have an URL like this:
http://mydomain/pictures/ajfhajkfhal/44/thumb.jpg
or
http://mydomain/pictures/ajfhajkfhal/44.jpg

This is rewritten to:
http://mydomain/Picture.aspx?group=ajfhajkfhal&id=44&thumb=thumb.jpg
or
http://mydomain/Picture.aspx?group=ajfhajkfhal&id=44

I added caching rules to IIS to cache JPG images when they are requested. This works with my images that are REAL images on the disk. When images are provided through the script, they are somehow always requested through the script, without being cached.

The images do not change that often, so if the cache at least is being kept for 30 minutes (or until file change) that would be best.

I am using .NET/C# 4.0 for my website. I tried setting several cache options in C#, but I cant seem to find how to cache these images (client-side), while my static images are cached properly.

EDIT I use the following options to cache the image on the client side, where ‘fileName’ is the physical filename of the image (on disk).

context.Response.AddFileDependency(fileName);
context.Response.Cache.SetETagFromFileDependencies();
context.Response.Cache.SetLastModifiedFromFileDependencies();
context.Response.Cache.SetCacheability(HttpCacheability.Public);
context.Response.Cache.SetExpires(DateTime.Now.AddTicks(600));
context.Response.Cache.SetMaxAge(new TimeSpan(0, 5, 0));
context.Response.Cache.SetSlidingExpiration(true);
context.Response.Cache.SetValidUntilExpires(true);
context.Response.ContentType = "image/jpg";

EDIT 2 Thanks for pointing that out, that was indeed a very stupid mistake ;). I changed it to 30 minutes from now (DateTime.Now.AddMinutes(30)).

But this doesnt solve the problem. I am really thinking the problem lies with Firefox. I use Firebug to track each request and somehow, I am thinking I am doing something fundamentally wrong. Normal images (which are cached and static) give back an response code “304 (Not Modified)”, while my page always gives back a “200 (OK)”.

alt text http://images.depl0y.com/capture.jpg

  • 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-15T01:41:56+00:00Added an answer on May 15, 2026 at 1:41 am

    If what you mean by “script” is the code in your Picture.aspx, I should point out that C# is not a scripting language, so it is technically not a script.

    You can use the Caching API provided by ASP.NET.
    I assume you alread have a method which contains something like this. Here is how you can use the Caching API:

    string fileName = ... // The name of your file
    byte[] bytes = null;
    
    if (HttpContext.Current.Cache[fileName] != null)
    {
        bytes = (byte[])HttpContext.Current.Cache[fileName];
    }
    else
    {
        bytes = ... // Retrieve your image's bytes
        HttpContext.Current.Cache[fileName] = bytes; // Set the cache
    }
    
    // Send it to the client
    Response.BinaryWrite(bytes);
    Response.Flush();
    

    Note that the keys you use in the cache must be unique to each cached item, so it might not be enough to just use the name of the file for this purpose.

    EDIT:

    If you want to enable caching the content on the client side, use the following:

    Response.Cache.SetCacheability(HttpCacheability.Public);
    

    You can experiment with the different HttpCacheability values. With this, you can specify how and where the content should be cached. (Eg. on the server, on proxies, and on the client)

    This will make ASP.NET to send the client the caching rules with the appropriate HTTP headers.
    This will not guarantee that the client will actually cache it (it depends on browser settings, for example), but it will tell the browser “You should cache this!”

    The best practice would be to use caching on both the client and the server side.

    EDIT 2:

    The problem with your code is the SetExpires(DateTime.Now.AddTicks(600)). 600 ticks is only a fraction of a second… (1 second = 10000000 ticks)
    Basically, the content gets cached but expires the moment it gets to the browser.

    Try these:

    context.Response.Cache.SetExpires(DateTime.Now.AddMinutes(5));
    context.Response.Cache.SetMaxAge(TimeSpan.FromMinutes(5));
    

    (The TimeSpan.FromMinutes is also more readable than new TimeSpan(...).)

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

Sidebar

Ask A Question

Stats

  • Questions 419k
  • Answers 419k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Look up fmod. The number of digits will probably be… May 15, 2026 at 10:14 am
  • Editorial Team
    Editorial Team added an answer I think, there is no such thing as a sphinx… May 15, 2026 at 10:14 am
  • Editorial Team
    Editorial Team added an answer Thread.CurrentCulture gets the info for the executing thread. ASP.NET code… May 15, 2026 at 10:14 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.