OdeToCode IC Logo

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.