Can I force a refresh of my stylesheet file?
I use this trick:
<link rel="stylesheet" type="text/css" href="cssfile.css?t=<%= DateTime.Now.Ticks %>" media="screen" />
For ASP.NET, the code behind (you can put this in a utility class or master page):
public static string GetTimestampedUrl(string virtualPath)
{
var realPath = HostingEnvironment.MapPath(virtualPath);
var file = new FileInfo(realPath);
return VirtualPathUtility.ToAbsolute(virtualPath) + "?" + file.LastWriteTime.ToFileTime();
}
And then in your page:
<link href="<%= GetTimestampedUrl("~/screen.css") %>" rel="stylesheet" type="text/css" media="screen" />
Press CTRL+F5 to hard-refresh everything on your webpage including scripts and stylesheets.
Additionally, you can incorporate the stylesheets to be served from a dynamic server page [php/asp.net] and the Response.Expires = -1 which will force the client to load the css on every HTTP-GET request explicitly. You can also do this in your webserver settings for CSS mime types.
A popular way of "cache-breaking" is to append a parameter to your css source. Typically a timestamp is used. I prefer the "file last modified" time, ie. filemtime()
in PHP. I'm sure there's an asp.net function that would give you that.
Then your CSS tag becomes:
<link rel="stylesheet" type="text/css" media="screen" href="/main.css?123456789"/>
with the query parameter changing whenever the CSS file is updated.