I'm trying to test some caching configuration, i want my page to stay in cache for 1 min before the request reaches the server again.
Using this simple test.asp page that has a link to itself:
<% Option Explicit %>
<%
Response.Expires = 1
Response.CacheControl = "private, max-age=60"
%>
<html>
<head><title>test</title></head>
<body>
<% =Now() %>
<br />
test
</body>
</html>
This works perfectly on my development computer http://localhost/test.asp, (clicking the link does not refresh the printed datetime during 1 min).
However it has not the desired effect when i put the page on the production server. After only few seconds of clicking the link I get a new datetime meaning the request reached the web server.
I use Chrome dev tool and see these response headers:
HTTP/1.1 200 OK
Cache-Control: private, max-age=60
Content-Type: text/html
Expires: Tue, 12 May 2015 19:16:52 GMT
Last-Modified: Tue, 12 May 2015 19:10:00 GMT
Server: Microsoft-IIS/7.5
X-Powered-By: ASP.NET
Date: Tue, 12 May 2015 19:15:55 GMT
Content-Length: 205
Can anyone help explain why it does not work on the prod server ?
update
I tried with Chrome, Firefox and IE, and also 2 pages test.asp and test2.asp, both having a link to the other page, and got exactly the same problem, after 8-12 sec, the page refresh instead of waiting 60sec before refreshing.
To follow up on my comment, It looks like you might be looking at caching your dynamic asp pages on the server, not on the client. Caching on the client doesnt really do you much good because modern browsers / proxies will still request the item when its an HTML document. Caching static resources which dont change such as images, css, js should work, and depending on the cache header you push out the browser will respect those.
To get your pages to cache on the server (meaning IIS doesnt have to re-generate the page) here is how you do it.
Web.config
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<caching>
<profiles>
<add extension=".asp" policy="CacheForTimePeriod" kernelCachePolicy="DontCache" duration="00:01:00" />
</profiles>
</caching>
</system.webServer>
</configuration>
You can place your web.config in a specific directory only to cache its contents, you can also break the caching using querystring params or certain request headers.
Related
I am at a loss to explain why my application is not caching the front page.
I have placed a datestamp on the page: #DateTime.Now
Here is the Home controller action
[OutputCache(Duration = 60)]
public ActionResult Index()
{
return View();
}
However, caching is not working and the front page keeps changing the date.
I have tried to reduce the front page and associated layouts to bare minimum so only the DateTime.Now was being stamped.
I have inspected all base controllers for any disable caching code.
Also inspected the global.asax
Inspected the web.config for any caching configuration - none.
Inspected with fiddler. Here is the response header.
HTTP/1.1 200 OK
Cache-Control: public, no-cache="Set-Cookie", max-age=60
Content-Type: text/html; charset=utf-8
Expires: Wed, 04 Mar 2015 05:20:45 GMT
Last-Modified: Wed, 04 Mar 2015 05:19:45 GMT
Vary: *
I have also tried different browsers that would not have any local caching settings modified.
Have also tried deploying the app to a stage server so it is not run on my dev machine.
Have tried making a fresh asp.net mvc application. That did work. Somewhat helpful in at least telling me the technology works.
Where could the problem be?
A couple of things to check:
If this is on your production environment, check output caching is enabled on IIS. See this page.
Try switching the location of your cache to help narrow down where the issue lies:
e.g.
[OutputCache(Duration = 60, Location = OutputCacheLocation.Server)]
[OutputCache(Duration = 60, Location = OutputCacheLocation.Client)]
Check your web.config for any [caching] entries which may override default behaviour
I have a default page that can display 3 different contents depending on whether:
The user is not logged in
The user is logged in but hasn't set something up yet
The user is logged in and has set something up
What's happening is that (1) and (2) will sometimes be displayed when it should be the other one. After I ctrl-r, the correct version displays.
This is nothing to do with the browser back button, it happens after clicking a menu option to go to the default page or performing some action that takes the user to the default page.
I also have a route set up for the page such that it's possible to append it with a username. eg: http://www.example.com/user1234
I mention this in case it may have something to do with it.
This is what I've tried to stop the caching:
<%# OutputCache Location="None" VaryByParam="None" %>
That didn't work so I tried (in Page_Load):
Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache)
Response.Cache.SetNoStore()
Any ideas what I'm doing wrong?
ETA
As per a couple of comments, I tried it in Chrome incognito mode and there is no problem. I tried with the console open as well to see the headers but unfortunately the problem goes away then. Here's the headers anyway:
Request
GET / HTTP/1.1 Host: localhost:2873 Connection: keep-alive
Cache-Control: max-age=0 Accept:
text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,/;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML,
like Gecko) Chrome/34.0.1847.92 Safari/537.36 Referer: .../signin
Accept-Encoding: gzip,deflate,sdch Accept-Language: en-US,en;q=0.8
Cookie: ...
Response
HTTP/1.1 200 OK Cache-Control: no-cache, no-store Pragma: no-cache
Content-Type: text/html; charset=utf-8 Content-Encoding: gzip
Expires: -1 Vary: Accept-Encoding Server: Microsoft-IIS/8.0
X-AspNet-Version: 4.0.30319 X-SourceFiles: =?UTF-8?B?...
X-Powered-By: ASP.NET Date: Thu, 27 Mar 2014 20:17:39 GMT
Content-Length: 3045
ETA2
I've been testing in Chrome and I've just found out there's no problem in FireFox.
If I login/out/in/out... there is no problem
If I login/out then log in with the wrong password a couple of times (causing postbacks in the login page), and then log in again correctly, it always displays the incorrect logout page in Chrome.
I was having the same problem. What I did was place this
Response.CacheControl = "No-cache";
within the page load before any of the other code runs. The issue that this solved for me was similar tor yours. When a user logs in, a drop down box is loaded with different stores that the user belongs too. If I changed their stores, they could navigate back to the page and see their old set of stores. I placed the above in the page load and this solved my issues within IE, Chrome and Firefox. Hopefully something as easy as this will fix your problem.
To resolve the caching issue with particular file you have to add
<location path="WebForm1.aspx">
<system.webServer>
<caching enabled="false" enableKernelCache="false" />
</system.webServer>
</location>
into your web.config's configuration section.
You can also do it using IIS. Steps to setup no cache using IIS is as below.
Go to your sites Content View. Right click on file and switch to features view.
With file selected open Output Caching open from IIS.
From Action Pane on right side click on Edit Feature Setting, and from dialog box untick Enable cache and Enable kernal cache. Click Ok to save setting into web.config.
take a look here and checking if it could be hopefull http://dotnet.dzone.com/articles/programmatically-clearing-0
I have a rendered HTML page that I am exporting to MS Word and downloading on a button click.
Here is the code snippet in the button click.
` Me.EnableViewState = False
Response.ContentType = "application/vnd.ms-word"
Response.AddHeader("Content-Disposition", "attachments;filename=XXXXXXX.doc")
Response.Buffer = True
Response.BufferOutput = True`
The functionality works perfectly well in Firefox & IE when I checked in system testing environment (locally). However when it was moved on to the hosting server (production environment) the functionality is not working in IE but is still working perfectly in Firefox.
I am not sure on where to check for the exact issue. Could it be a caching related problem?
In IE it is just not opening the download window which we will obtain when the rendered HTML content type is changed and the response stream is flushed. No exception is thrown.
I received the following response header:
HTTP/1.0 200 OK
Cache-Control: private
Content-Length: 15189
Content-Type: application/vnd.ms-word;
charset=utf-8 Server: Microsoft-IIS/7.5 X-AspNet-Version: 2.0.50727
Content-Disposition: attachments;filename=NewAccountForm.doc X-Powered-By: ASP.NET
Date: Fri, 18 Mar 2011 10:18:07 GMT X-Cache: MISS from Gateway X-Cache-Lookup: MISS from Gateway:808 Via: 1.0 Gateway (squid/3.0.STABLE10) Proxy-Connection: keep-alive
Is your hosted environment adding http headers? IIS could easily be configured to add headers that are messing up what you want to send out. It's actually quite common, and could be the issue. You'll need to determine this for sure, so here's a suggestion for investigating:
Try using a WebClient class and look at the Response headers.
Edit - updated
I just noticed - did you remember to put
Response.Clear();
before adding headers? It may not be the hosting environment at all. However, given that it works locally and not at your hosting environment, and assming that it is the same code, it still looks to me like something is different about the hosted environment, and the most logical option would be the headers.
I set Response.Charset = "" and always do Response.Flush(), Response.End(), and Response.Close() after I export the HTML.
I'm not sure whether this is a server issue, or whether I'm failing to understand how HTTP caching really works.
I have an ASP MVC application running on IIS7. There's a lot of static content as part of the site including lots of CSS, Javascript and image files.
For these files I want the browser to cache them for at least a day - our .css, .js, .gif and .png files rarely change.
My web.config goes like this:
<system.webServer>
<staticContent>
<clientCache cacheControlMode="UseMaxAge"
cacheControlMaxAge="1.00:00:00" />
</staticContent>
</system.webServer>
The problem I'm getting is that the browser (tested Chrome, IE8 and FX) doesn't seem to be caching the files as I'd expect. I've got the default settings (check for newer pages automatically in IE).
On first visit the content downloads as expected
HTTP/1.1 200 OK
Cache-Control: max-age=86400
Content-Type: image/gif
Last-Modified: Fri, 07 Aug 2009 09:55:15 GMT
Accept-Ranges: bytes
ETag: "3efeb2294517ca1:0"
Server: Microsoft-IIS/7.0
X-Powered-By: ASP.NET
Date: Mon, 07 Jun 2010 14:29:16 GMT
Content-Length: 918
<content>
I think that the Cache-Control: max-age=86400 should tell the browser not to request the page again for a day.
Ok, so now the page is reloaded and the browser requests the image again. This time it gets an empty response with these headers:
HTTP/1.1 304 Not Modified
Cache-Control: max-age=86400
Last-Modified: Fri, 07 Aug 2009 09:55:15 GMT
Accept-Ranges: bytes
ETag: "3efeb2294517ca1:0"
Server: Microsoft-IIS/7.0
X-Powered-By: ASP.NET
Date: Mon, 07 Jun 2010 14:30:32 GMT
So it looks like the browser has sent the ETag back (as a unique id for the resource), and the server's come back with a 304 Not Modified - telling the browser that it can use the previously downloaded file.
It seems to me that would be correct for many caching situations, but here I don't want the extra round trip. I don't care if the image gets out of date when the file on the server changes.
There are a lot of these files (even with sprite-maps and the like) and many of our clients have very slow networks. Each round trip to ping for that 304 status is taking about a 10th to a 5th of a second. Many also have IE6 which only has 2 HTTP connections at a time. The net result is that our application appears to be very slow for these clients with every page taking an extra couple of seconds to check that the static content hasn't changed.
What response header am I missing that would cause the browser to aggressively cache the files?
How would I set this in a .Net web.config for IIS7?
Am I misunderstanding how HTTP caching works in the first place?
You need to use the Expires directive, otherwise the browser will always check to see if the content has updated.
If a cached entry has a valid expiration date the browser can reuse the content without having to contact the server at all when a page or site is revisited. This greatly reduces the number of network round trips for frequently visited pages. For example, the Google logo is set to expire in 2038 and will only be downloaded on your first visit to google.com or if you have emptied your browser cache. If they ever want to change the image they can use a different image file name or path.
To change in IIS7 use following. This is easiest to manage if you keep static content in specific directories.
Log onto the server
Open IIS Manager (start -> adminstrative tools -> iis manager
Expand the server node
Expand the sites node
Open the site and navigate to the directory you want to change
Open the IIS HTTP Response Headers section
Click Set Common Headers on the task pane on the right
Set "Expire Web Content" as your app requires.
use expires header instead of using the cache-control.Tell your server for the first time that serve me content from my browser cache until this expiry date. There will be no cross checking for changes in file until your expiry date.
add the header in your web.config’s system.webServer section like so:
<system.webServer>
<staticContent>
<clientCache httpExpires="Sun, 29 Mar 2020 00:00:00 GMT"
cacheControlMode="UseExpires" />;
</staticContent>
</system.webServer>
Short anwser: remove Etag and use Expire header.
You should check out the 35 Yahoo Performance best practices, and more specifically:
Configure ETags on your server (removing them is a good choice)
Add Expire header for static resources
For each rule, they usually cover Apache and IIS web server configurations.
Edit: okay, it looks like there is no simple way to remove Etags in IIS, besides installing some 3rd party software...
I'm seeing an issue of some static pages that are using the browser cache, which is not desired. To prevent caching, I'm setting
<clientCache cacheControlMode="DisableCache" />
in the relevant <location> tag in web.config
If I open the page in Firebug (in the Net tab), I see that the Response headers have Cache-Control: no-cache which is correct, but the status of the Response is 304 Not Modified! Isn't that a contradiction? How can I get it to stop caching (i.e. always send a 200 with content)?
According to the RFC (http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9.1, section 14.9.1) Cache-control: no-cache tells the browser to not use the cached contents without first revalidating with the server. That's why you're seeing the 304's. I think you're looking for Cache-Control: no-store.
I'm not sure if you can send no-store via a configuration file. You can, however, set the header explicitly in the response:
Response.Cache.SetNoStore();
EDIT: (by OP)
What I was looking for was:
<clientCache cacheControlCustom="no-store" />