I'm working on a site that uses ASP.NET Themes. The problem I'm having is that I've updated the CSS file, but the original file is being held by the users' browsers. I'm wondering if there's a way to force the browser to get the latest CSS file.
The Theme is set by code similar to:
private void SetPageTheme()
{
Page.Theme = "ClientA_Theme";
}
I've seen other answers to similar questions which suggest that I append a version number to the CSS file, e.g. /CssFile.css?v=1.2.3
Is there a way for me to do this with ASP.NET Themes?
I had a similar problem; you can either set up IIS to add a content-expiration header for the CSS files so they never get cached, or you can use the solution presented in this SO question here
You can also add a method to each of your pages to go through the controls in your page.header (perhaps even in your Master.Page!) to do something like:
protected override void OnPreRender(System.EventArgs e)
{
foreach (Control link in Page.Header.Controls)
{
if (link is HtmlLink)
{
HtmlLink cssLink = link as HtmlLink;
//Check if CSS link
if (cssLink.Attributes["type"].Equals("text/css", StringComparison.CurrentCultureIgnoreCase))
{
if (cssLink.Attributes["href"].Equals(String.Format("~/App_Themes/{0}/{0}.css", Page.Theme), StringComparison.CurrentCultureIgnoreCase))
{
//perhaps add the version of your app here.
cssLink.Attributes["href"] += "?v1.1";
}
}
}
}
base.OnPreRender(e);
}
Another technique would be to include a version number in the theme name - for example MyTheme1_1 or similar. Each release would then be loading content from a "new" url and hence the content should be requested again for each user. Obviously, this is more work, but is should be once per release and doesn't have the overheads you mention below.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With