Right way to have ASP.NET / IIS NOT cache PDF files

GR7 picture GR7 · Nov 22, 2011 · Viewed 13.3k times · Source

I have the following scenario, and I wanted suggestions on what is the best way to handle this. My web app (ASP.NET 2.0 / IIS 6) generates PDF files, and I have a results page with links to those PDFs.

Now, I noticed that if I visit the results page, click on a PDF file (it opens in a new window), then re-generate the PDF file, and click on the same link in the results page, the OLD PDF is shown, instead of the new one. I had to delete the temporary internet files in order to see the new one.

So, since I'm NOT serving an ASPX that actually writes the PDF (and I do not want the Save dialog to show), but straight linking to the PDF file, I want to know what the best way to make sure the user always sees the latest file in the server, not a cached version.

I'm guessing adding no-cache headers is out of the question. But the PDF request would still go through an HTTP handler, so I'd like to know if I should create a specific HTTP handler to intercept requests for PDFs, or if i should do this at the IIS level...however I dont necessarily want to avoid caching ALL PDF's on that site.

Any suggestions? Thanks in advance for the help.

Answer

Neil Thompson picture Neil Thompson · Nov 22, 2011

If your link to the pdf document had a unique querystring appended I believe that would prevent caching. Time in ticks is a good one to use, eg:

string.Format("{0}?t={1}", pdfFileUrl, DateTime.Now.Ticks);