A
common, and important, requirement of implementing web applications is to
version them. Very often the customer themselves care little for the version,
but to the QA and Development teams that support them, it is extremely
important. Particularly where version control is employed (a fundamental basic
principal of good development) and branches of the application must be
developed, whilst continuing to maintain the main code trunk, or where multiple
versions of the software are available.
This
post will show you how to detail the names and versions of assemblies that the
web app is using in ASP.NET MVC3.
Basic
Requirement
At
its most basic form the version of the web app itself should be present
somewhere for the user to see, such as the About page. This allows them to
report a potential bug and the version of the app it has occured in.
Start
Visual Studio 2012 and create a new ASP.NET MVC3 Web Application named
MyMVCApplication. The default application structure provides various
Controllers and Views. As we want to add the version information to the about
page, locate the About() method in the HomeController and add the following
code:
The
controller identifies the name and location of the current applications
assembly, extracts the name and version and places the details in the ViewBag
ready for the View to display.
The
About View needs to display the Assembly and Version stored in the ViewBag:
Now
build and run the application, navigate to the About page and, voila, the
current version is displayed.
Improvements
We're
using ASP.NET MVC3 here, so we know that this isn't the only assembly being
used! The Views are using Razor to output the content of the ViewBag for a
start - and Razor resides in its own assembly: System.Web.Razor. It might be
useful to list all the assemblies used by the web app in the About page. After
all, the more information relating to the version and a reported bug, the
simpler it is to locate and fix.
So,
lets take the example above a little further to display all the assemblies.
The
About controller will need tweaking to discover all the assemblies used and to
store the name and version ready for display on the View. We'll define a string
dictionary to store the name and version of each assembly as a key value pair,
store the dictionary in the ViewBag and call the View.
The
About view must also cater for multiple assemblies. We'll simply iterate over
the ViewBags' Assemblies property, and output the name (key) and version
(value) onto the page.
Build
and run the application, navigate to the About page and, voila, all the
assemblies currently used in the web app are displayed.
Written by Dan Hacker, Software Developer, DSCallards
For more information, visit www.dscallards.com.
No comments:
Post a Comment