Displaying the Build Date on your Web Pages

The Build Date Problem

I have struggled to find a good way to communicate with users the version of a web site. Version numbers are tricky and only meaningful to people close to the project. I like dates but they are often tricky to implement.

My default choice has always been dates because they are easy for customers to understand. They can look and see if there has been an update recently. However, the only way I have know to do this was to have some sort of pre- or post-build step that would inject the current date and time into a file in the application. While this works it is a hassle and can have some undesirable side effects during development depending on the implementation.

A Build Date Solution

I recently found a great solution to this problem while working on an ASP.NETCore project, but it should work with any .NET project. The key principle here is that .NET linker stamps a date on the assembly. This date is not easy to get to, but thanks to Jeff Atwood for a great post about it: https://blog.codinghorror.com/determining-build-date-the-hard-way/

By using his code and shaping it for a web environment I was able to create a class that will return the date as a static property making the assumption that this only needs to be determined once each time the site is started.

A Reusable Build Date Class

Here is the code for the AssemblyInfo class that exposes a Date property to return the linker date.

Updating Your View with the Build Date

This method can then easily be called in your _layout.cshtml page to show the date of the deployment on every page. In addition, you can also choose to show it on an about page or other location of your choice. If you want consistent formatting for multiple locations, you could create another method that provides a formatted string.

I hope this helps you better communicate with your users about which version of a page they are seeing.

Update for Visual Studio 15.4.

Builds in VS now contain information that makes the values in these fields gibberish. Fortunately, this can be easily fixed with a change to your .csproj file. in the first <PropertyGroup> section add the following element:

Thanks to Tom Puckett for the note on this below.

Join the Conversation

2 Comments

Your email address will not be published. Required fields are marked *

  1. Sorry, the article has removed from my comment the tags which the false value is used, the tag is ‘Deterministic’, to be added inside a ‘PropertyGroup’ tag.

  2. A recent roadblock with the release of Visual Studio 15.4 is the change in default build parameters to use deterministic compilation. This means that the timestamp field in the assembly header no longer reflects the date or time of the build, invalidating this useful solution. This change can be overridden by adding false inside a element of the .csproj file, though this defeats the “progress” intended by the change in default behavior. I tried to implement an alternative by referencing a build variable in the assembly copyright attribute, but the tried and true $(Date) macro has disappeared from the build system and I ultimately failed to get something working.