Search code examples
asp.netmsbuildwebformsversioningcache-control

Adding MSBuild version to ASP.NET Web Project in VS2012


I'm working on a Web Site solution (not a Web Application shame on me) in Visual Studio 2012 on a ASP.NET Web Forms (double shame on me) project. I would like to have an auto-increment version on each build so that I can retrieve it in my code and use for static files versioning (mostly css and js) to avoid chache it.

What I want to obtain are links like /mydir/mystyle.css?v=buildversionhere

I found a nice code to retrieve the version:

Dim web As Assembly = Assembly.GetExecutingAssembly()
Dim webName As AssemblyName = web.GetName()
Dim myVersion As String = webName.Version.ToString()

And it works, the problem is I always get 0.0.0.0. What I need to know is how to set an auto-increment on each website build, like where to set something like this:

[assembly:AssemblyFileVersion("1.0.*")]

Remember I'm on a Web Site project not on a Web Application. Thanks in advance


Solution

  • OK.. after some in-depth research and some head-crushing since documentation (official) is more than often very superficial I found a way to do it. So if you have a WEB SITE project and want to add semi-automatic versioning to your web site (maybe to retrieve this version for whatever usage you want like canceling static files cache appending ?v=version) here is how to do it:

    • Create a file in your project NOT INSIDE THE App_Code folder and name it like AssemblyInfo.vb (I'm coding in VB. If you need C# search "Telerik Code Converter" in Google). You can put the file inside App_Data folder if you want
    • Inside this file you don't need to create any class, just specify infos for your .DLLs inserting this code:

    Example

    Imports System.Reflection
    
    <Assembly: AssemblyDescription("desc")> 
    <Assembly: AssemblyCompany("company")> 
    <Assembly: AssemblyCopyright("Copyright © 2013 your name")> 
    <Assembly: AssemblyProduct("asdasd")> 
    <Assembly: AssemblyVersion("2.0.*")> 
    

    Then inside your web.config (between the "configuration" tags) insert this code:

    <system.codedom>
     <compilers>
      <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.VisualBasic.VBCodeProvider, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
        compilerOptions='"C:\Absolutepath\App_Data\AssemblyInfo.vb"' />
     </compilers>
    </system.codedom>
    

    with the path to your AssemblyInfo.vb file. ATTENTION: if you don't want to waste hours like me... this path has to be ABSOLUTE and cannot be relative. So when you move your site in production on another server remember to change it.