Automatic SVN Revision Numbering in ASP.Net MVC

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.

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:

Visual Basic .NET

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

ImportsSystem.Runtime.CompilerServices

PublicModuleHtmlHelpers

<extension()>_

PublicFunction SVNRevision(ByValhelper AsHtmlHelper)AsString

DimsvnFile AsIO.StreamReader

DimsvnRev AsString=String.Empty

Try

svnFile=NewIO.StreamReader("svn_rev.txt")

svnRev=svnFile.ReadLine()

svnRev=svnRev.Replace("""","")

svnFile.Close()

Catch FnFex AsIO.FileNotFoundException

'swallow, but write out the file and location we tried to read..

Trace.WriteLine("HtmlHelper.SVNRevision: Ex: ["&FnFex.Message&"]")

Finally

If svnRev.Length=0Then svnRev="-"

EndTry

Return svnRev

EndFunction

EndModule

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 line to the “Post-build event command line:” box..

Shell

1

call"$(ProjectDir)CreateSvnRevFile.bat""$(SolutionDir)\"

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:

XHTML

1

2

3

4

5

...

<%@importNamespace="MyMvcProject"%>

...

<%=Html.SVNRevision()%>

...

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 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.

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.