Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force CSS refresh when using ASP.NET Themes?

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?

like image 933
DaveDev Avatar asked Dec 06 '11 10:12

DaveDev


1 Answers

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.

like image 169
dash Avatar answered Sep 21 '22 16:09

dash