AssemblyVersion and Web Projects

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;

[assembly:
AssemblyVersion("2.1.1.2")]

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;
  }
  
</script>

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

The above web form will happily display the version number as 0.0.0.0 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.

posted on Tuesday, January 24, 2006 11:28 PM by scott

Comments

Wednesday, January 25, 2006 5:13 AM by Jason Haley

# Interesting Finds

Wednesday, January 25, 2006 11:43 AM by Gregor Suttie

# re: AssemblyVersion and Web Projects

Thanks for yout reply to this - works a treat.

Cheers
Gregor
Thursday, January 26, 2006 6:38 PM by Christopher Steen

# Link Listing - January 26, 2006

ASP.NET Podcast Show #35 - Michael Mahemoff of
AjaxPatters.org and SoftwareAs.com. [Via:
http://www.scalabledevelopment.com/mcclure.htm...
Thursday, February 02, 2006 12:11 PM by SteveC

# re: AssemblyVersion and Web Projects

That is great info, thanks for making the post.

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

Thursday, April 20, 2006 3:47 AM by Jason Haley

# Interesting Finds

Thursday, April 20, 2006 8:52 AM by John Adams

# re: AssemblyVersion and Web Projects

Not sure about the article is about as it has no intro. Other articles such as...
http://www.odetocode.com/Articles/450.aspx 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.

John
Thursday, April 20, 2006 8:56 AM by scott

# re: AssemblyVersion and Web Projects

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.
Monday, July 10, 2006 9:35 AM by AKumar

# re: AssemblyVersion and Web Projects

Wondering how one can obtain compile time stamp so that besides the version #, the compile date is obtainable

Kumar