Automatic SVN Revision Numbering in ASP.Net MVCSun 14 December 2008 0 Comments
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 Cruise Control.Net..
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”)
As shown above, we need to add the following 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
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 usefulcomments