views:

39

answers:

3

I would like to give my CSS and javascript files far-future headers and add a token onto the URLs referrring to them. That way browsers don't have to re-download CSS and javascript unless I've released a new build of the site.

http://example.com/css/styles.css?build=23424

How can I pass a build number or timestamp to my code-behind so it can add the timestamp?

Obviously C# doesn't have macros, which is what I would use in C/C++.

I realise that this will force browsers to download assets whenever I do a new build - regardless of whether or not the build involved changing the assets. However, I would like to put a simple scheme in place before I implement anything more advanced like looking at individual file modification times.

+2  A: 

Here's a bit of code that I use to extract the build id from the current assembly at application start. It reads the version number from the assembly, a version designator (dev/qa/blank) from the web config, then constructs a version number string to stuff into the application. This code goes in Global.asax.cs. You can then refer to it in your markup from the Application instance.

var webAssembly = Assembly.GetAssembly( typeof(...known class...) );
var designator =  WebConfigurationManager.AppSettings["VersionDesignator"];
string version = webAssembly.GetName().Version + designator;
this.Application.Add( "Version",  version );

Here's an example of how you could use it in an MVC context (sorry I don't have any WebForms examples).

<link type="text/css" rel="stylesheet"
      href="/Content/styles/screen.css?build=<%= this.Application["Version"] %>" />
tvanfosson
+1  A: 

Interesting idea.

Here's one way to do it:

  1. Go into your AssemblyInfo.cs class under the Properties folder in your project.
  2. Change your assembly version to include the star wildcard: [assembly: AssemblyVersion("1.0.*")].
  3. In your code, you can retrieve the current build version like this:

_

var buildNumber = Assembly.GetExecutingAssembly().GetName().Version.Build;

_

That's it, you're done.

Changing the AssemblyVersion to 1.0.* will cause Visual Studio/MSBuild to increment the build number automatically for you each build. Then, you can access this build number at runtime using the above code snippet. Easy as cheese.

Judah Himango
But there is no reason to do it this way. It's not like auto incrementing build number is any different than a hard set number for this instance. Would be easier to leave the build nuber as-is and Just use your buildNumber property to use is as needed.
Tim Meers
A hard set number has to be manually incremented.
Judah Himango
A: 

Browsers and servers know how to handle caching via HTTP. So, I misunderstand, what are you trying to accomplish with providing this kind of caching. A browser will not re-download your CSS and Javascript if it has seen it recently. And it will re-download it if you do a push of your new build on the server. See cache headers like Cache-control and Expires etc. Here's a tutorial http://www.mnot.net/cache%5Fdocs/

harschware