5

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?

DaveDev
  • 41,155
  • 72
  • 223
  • 385

1 Answers1

6

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.

Community
  • 1
  • 1
dash
  • 89,546
  • 4
  • 51
  • 71
  • Interesting, but too much cost of cpu in every page, every time, for ever, just for ones time update. – Aristos Dec 06 '11 at 11:12
  • @Aristos Then the only other thing I can think of you doing is putting the version number of your app in the theme name. At least that way each release will use a distinctive version of each theme, which means that new requests will be issued for that content. – dash Dec 06 '11 at 11:29
  • I use a complex class that reads all css, minimize them, add version on file, and cache it (on database) one time only in every update, then just read the ready existing file. The asp.net 4.5 have a similar inbuild function. – Aristos Dec 06 '11 at 13:45