April 21st 2008
YSlow and ASP.NET – Expires Header – BuildProvider (Part 3 of 3)
For the last part of our quest to enable caching of static content by separating it out into directories by version, we want to provide an easy and intuitive way for developers to reference static content from the code behind. e.g.
Dim linkIcon As New System.Web.UI.WebControls.Image linkIcon.ImageUrl = StaticContent.Images.rss_gif
Under the covers StaticContent.Images.rss_gif resolves to a method call -StaticContent.PathBuilder.ConvertImageUrl(“rss.gif”) (the method is covered in part 2).
To do this we need a buildProvider (Note: this is not supported in Web Application Projects (WAP) see notes at the end).
The code is mostly written for us on Dave Transom’s excellent post.
The slightly modified code is here.
Now that we have a buildProvider we wire it into the build using the web.config:
<compilation debug="true" strict="true" explicit="true"> <buildProviders> <add extension=".hrefs" type="Savo.Framework.Web.StaticContent.BuildProvider"/> </buildProviders> </compilation>
To configure our BuildProvider create a file with the extension “.hrefs” in your App_Code directory, using some variation on the following:
<?xml version="1.0" encoding="utf-8" ?>
<settings
namespace=""
className="StaticContent"
minDepth="1"
maxDepth="100"
lowercaseUrls="true"
includeExtension="true"
truncatePathToStartingDepth="true"
resourceResolutionFormat='StaticContent.PathBuilder.ConvertStaticContentUrl("{0}")'
directoryNamesToIgnore="StaticContent">
<files include="\.(gif|jpg|png|css)$" exclude="">
<folders include="StaticContent" exclude="App_|Bin|\.svn">
</settings>
If you’re using a “Web Application Project”, this method won’t work as per this MS post. My suggestion is to use a pre-build event (although this has a few extra gotchas) or another framework like slink though I haven’t tried either method.