AssemblyVersion and Web Projects

Wednesday, January 25, 2006

Let’s say you want to add an AssemblyVersionAttribute to a web project. The usual practice is to add a file to the project with the name AssemblyInfo.cs. The AssemblyInfo.cs file will live in the App_Code folder, because that is the only location the new web project model will allow stand-alone code files. The contents might look like:

using System.Reflection;


Next, a web form to display the version number:

<%@ Page Language="C#" %>
@ Import Namespace="System.Reflection" %>

<script runat="server">
protected void Page_Load(object sender, EventArgs e)
Assembly assembly = Assembly.GetExecutingAssembly();          
string version = assembly.GetName().Version.ToString();    
    versionLabel.Text = version;

<asp:Label runat="server" ID="versionLabel" />

The above web form will happily display the version number as because App_Code compiles into a different assembly than the web form. The App_Code assembly will have the version number we need. The ASP.NET compiler doesn’t assign a version number to the dynamically generated assemblies for web forms. This is true in 1.1, too, it’s just that we usually asked for the version number from a code-behind file, and all the code-behind files compiled together into a single assembly.

There are a couple approaches to getting the version number from the AssemblyVersionAttribute in App_Code. One approach would be to substitute the following in our earlier code:

Assembly assembly = Assembly.Load("App_Code");
string version = assembly.GetName().Version.ToString();  

I think an even better approach is to use the Web Deployment Project, which can merge all assemblies into a single assembly, and copy assembly level attributes over, too. If you are using the newer Web Application Project model, then any code in a code-behind file will be able to see the version in an AssemblyVersion source file.

Gregor Suttie Wednesday, January 25, 2006
Thanks for yout reply to this - works a treat.

SteveC Thursday, February 2, 2006
That is great info, thanks for making the post.

Now if I can just get my head around automating the build + deployment via script....

John Adams Thursday, April 20, 2006
Not sure about the article is about as it has no intro. Other articles such as... have excellent intros so you have an idea what the article is about. I don't want to be picky, as I am sure the information is great, but most people would be so lost coming to this page.

scott Thursday, April 20, 2006
John: true. I consider the blog posts to be a little more informal. I don't spend nearly as much time making a proper 'article' out of these posts, which is both good and bad.
AKumar Monday, July 10, 2006
Wondering how one can obtain compile time stamp so that besides the version #, the compile date is obtainable

Comments are now closed.
by K. Scott Allen K.Scott Allen
My Pluralsight Courses
The Podcast!