Add url parameter to css file in asp themes folder

I wrote some code that helps with the version of js files. Essentially, it revolves around the current script manager and adds the path to the javascript file with the hash file md5. So,

<script src="../Javascript/Navigation.js" type="text/javascript"></script> 

becomes

 <script src="../Javascript/Navigation.js?md5=70D2B4D1F236C7E340D9152B9E4102C3" type="text/javascript"></script> 

I think this is a fairly common thing (or its variants). What I'm struggling to do is collect the css files in the app_themes folder and do the same.

How do I login and change css links?

+6
source share
3 answers

You can use the management adapter to gently inject this behavior into the page as follows:

 public class PageAdapter : System.Web.UI.Adapters.PageAdapter { protected override void OnPreRender(System.EventArgs e) { foreach (var link in Page.Header.Controls.OfType<HtmlLink>().ToList()) if (link.Attributes["type"].Equals("text/css", StringComparison.OrdinalIgnoreCase)) if (link.Attributes["href"].Contains("/App_Themes/{0}/".Fill(Page.Theme), StringComparison.OrdinalIgnoreCase)) /* process link */ base.OnPreRender(e); } } 

You can connect it by saving the following in a * file . browser in the App_Browsers folder:

 <browsers> <browser refID="Default"> <controlAdapters> <adapter controlType="System.Web.UI.Page" adapterType="PageAdapter" /> </controlAdapters> </browser> </browsers> 

Overall, I think Control Adapters are a powerful AOP-like mechanism for injecting behavior into control / page life cycles; they are almost completely ignored in favor of the traditional subclass.

+6
source share

I ran into one problem that it repeats the css entry in the html markup on every postback. For example, I have newabc.css. will the code change it to newabc.css? v = 1. if I see the html source after 5 postback, it will have 5 "newabc.css? v = 1". so I added link.EnableViewState = False, it works fine, but is it really needed?

  Dim link As HtmlLink = Nothing For Each c As Control In Page.Header.Controls If TypeOf c Is HtmlLink Then link = TryCast(c, HtmlLink) If link.Href.IndexOf("App_Themes/", StringComparison.InvariantCultureIgnoreCase) >= 0 AndAlso link.Href.EndsWith(".css", StringComparison.InvariantCultureIgnoreCase) Then link.Href &= "?v=" & VER_CSS 'link.EnableViewState = False End If End If Next 
+2
source share

Avoid reinventing the wheel twice using Combres . Does everything you ask and much more!

+1
source share

All Articles