• Automatic SVN Revision Numbering in ASP.Net MVC

    by  • December 14, 2008 • 3 Comments

    Update: I’ve written an open source .Net build tool that can also put the highest SVN revision into a file automatically on build – check it out!


    While working on an internal ASP.Net MVC project at work, I wondered if it was possible to get the SubVersion (SVN) repository number to be automatically updated on the webpages I was creating every time I did a build or check-in.

    The footer at the bottom of the StackOverflow.com site is what we’re trying to do here (though I suspect after listening to their podcasts that they’ve got theirs updated using custom build tasks in CruiseControl.Net)..

    StackOverflow.com - SVN revision

    After a couple of hours of investigation, trial and error, here’s one way to do it.

    Before we begin, we’ll need TortoiseSVN installed, and a local working copy of your ASP.Net MVC project checked out.

    There are essentially 3 steps..

    1. Create a Html Helper

    This greatly simplifies the return of the SVN number as a string which is then trivial to place in our View Page or footer.

    Create the following file:
    [vbnet]
    Imports System.Runtime.CompilerServices

    Public Module HtmlHelpers

    _
    Public Function SVNRevision(ByVal helper As HtmlHelper) As String
    Dim svnFile As IO.StreamReader
    Dim svnRev As String = String.Empty
    Try
    svnFile = New IO.StreamReader(“svn_rev.txt”)
    svnRev = svnFile.ReadLine()
    svnRev = svnRev.Replace(“”"”, “”)
    svnFile.Close()
    Catch FnFex As IO.FileNotFoundException
    ‘swallow, but write out the file and location we tried to read..
    Trace.WriteLine(“HtmlHelper.SVNRevision: Ex: [" & FnFex.Message & "]“)
    Finally
    If svnRev.Length = 0 Then svnRev = “-”
    End Try

    Return svnRev
    End Function
    End Module
    [/vbnet]

    2. Create a Batch File and add it as a custom build task to the project

    Next we create a Batch File (.bat) in the root of our project, which will call the TortoiseSVN command to retrieve the SVN revision number from our working copy.
    We’ll call it “CreateSvnRevFile.bat”. Here’s the contents:

    Then we need to add a custom Build Event action to call the Batch File. (The Build Events dialog is in “My Project > Compile > Build Events”)

    My Project - Compile - Build Events

    As shown above, we need to add the line to the “Post-build event command line:” box..

    3. Add the SVN revision number to our Page

    With all of that background plumbing in place, all we need to do now is add a couple of lines of code in the appropriate .aspx file wherever we want our SVN revision number to appear:

    the first line imports our Project Namespace (which will obviously depend on your project – you may need to rebuild at this point before the =Html. autocomplete picks up the ‘SVNRevision()’ HtmlHelper).

    And we’re done:

    So, how does it work?

    The Post-build task CreateSvnRevFile.bat does most of the work by creating a temporary text file called svn_rev.txt containing the string “$WCREV$”, and then calls the TortoiseSVN command SubWCRev.exe on the text file to replace the string with the working copy revision number.  It’s worth noting that the revision number is held in the working copy .svn folder so this method doesn’t require a call to your SVN server to work.

    The Html Helper code then simply tries to read the updated text file and returns the contents as a string, or it returns a dash (“-”) if there was a problem.

    Obviously, if we’re using this on a high-traffic website or frequently referenced page then we’d want to cache the string rather than reading it from the file every time. We could do this by modifying the Html Helper to read the file once into a variable, which we could do by wrapping the actual file reading up in a singleton object, for example.

    I hope you find it useful :)

    About Andrew Freemantle

    3 Responses to Automatic SVN Revision Numbering in ASP.Net MVC

    1. February 8, 2009 at 3:22 am

      Cool. Thanks. I didn’t know about the SubWCRev.exe tool. I think I will put the $WCREV$ placeholder in an app setting in my web config though.

    2. Richard Fleming
      February 25, 2009 at 10:06 pm

      I don’t know ASP very well but do have another approach that I use on my HTML/PHP pages.

      Using svn:keywords with Revision on *.html I have two snippets of code that are in a header and footer template

      In the header I set a variable as $svnRevision = ‘$Revision$’ that gets replaced with the file’s revision on every check-in (as well as an include to a function library)

      In the footer I call a function from my library ‘returnSvnRevision($svnRevision)’ that cuts from the front ‘$Revision: ‘ and ‘ $’ from the back leaving me with just the revision number to display any way I want.

    3. February 26, 2009 at 9:02 pm

      @Richard – thanks for your comment :)

      I may be wrong here, but I think that keyword substitution gives the revision of the specific file containing the substitution, rather than the latest revision of the last file checked-in for the entire project.

      Source: http://svnbook.red-bean.com/en/1.4/svn.advanced.props.special.keywords.html

    Leave a Reply

    Your email address will not be published.

    Your name *

    Your website

    *