Automatic SVN Revision Numbering in ASP.Net MVC

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:

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 :)

4 thoughts on “Automatic SVN Revision Numbering in ASP.Net MVC

  1. Richard Fleming

    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.

Leave a Reply

Your email address will not be published.

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">