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.

Leave a Reply

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