Monday, January 4, 2010

Adding AssemblyVersion to ASP.net MVC Site using Helpers

Introduction


I was looking around for some information on how best to add the AssemblyVersion to an MVC site. In the end I thought I'd do it my self as it can't be too difficult. It wasn't and I've included how to do it in this post to help other people out.

Adding a Helper Class to MVC project

The first thing to do is add a Helper class to your existing project. I decided not to use a namespace so I didn't have to import the namespace into the View.



using System;
using System.Web.Mvc;
using System.Reflection;

//
// No Namespace
//

public static class VersionHelper
{

    /// <summary>
    /// Return the Current Version from the AssemblyInfo.cs file.
    /// </summary>
    public static string CurrentVersion(this HtmlHelper helper)
    {
        try
        {
            System.Version version = Assembly.GetExecutingAssembly().GetName().Version;
            return version.Major + "." + version.Minor + "." + version.Build;
        }
        catch (Exception)
        {
            return "?.?.?";
        }
    }
}

Adding the Code to your view

Once you've added the above Html helper class you'll be able to reference the CurrentVersion() Method using intellisense.



<span>Version <%=Html.CurrentVersion() %></span>

Conclusion

That's it. You can now reference the version from anywhere in any of your views. Here is what it looks like on the default MVC project.




Also if you want the code, you can download it from here.

30 comments:

  1. Made a slight modification to your code by putting it into the constructor of a Lazy object, like so.

    private static Lazy version = new Lazy(() =>
    {
    try
    {
    System.Version version = Assembly.GetExecutingAssembly().GetName().Version;
    return String.Format("{0}.{1}.{2}.{3}",
    version.Major,
    version.Minor,
    version.Build,
    version.Revision);
    }
    catch (Exception)
    {
    return "?.?.?.?";
    };
    });

    ///
    /// Returns the Current Version from the AssemblyInfo.cs file.
    ///
    public static string CurrentVersion(this HtmlHelper helper)
    {
    return version.Value;
    }

    This guarantees that the code only runs once per and not every time a page is rendered. It's a small optimization, but I figured there is no reason to calculate the same value repeatedly.

    ReplyDelete
  2. This might interest those who call their helper from a separate project and do not receive the correct version number: http://stackoverflow.com/a/2815382/41211

    ReplyDelete
  3. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a .Net developer learn from Dot Net Training in Chennai. or learn thru ASP.NET Essential Training Online . Nowadays Dot Net has tons of job opportunities on various vertical industry.
    or Javascript Training in Chennai. Nowadays JavaScript has tons of job opportunities on various vertical industry.

    ReplyDelete
  4. hi there,i got wanted to me.so thanks for that.be helpful
    dot net training chennai

    ReplyDelete

  5. تنظيف منازل بالدمام تنظيف منازل بالدمام
    تنظيف منازل بالاحساء تنظيف منازل بالاحساء
    تنظيف منازل بمكة تنظيف منازل بمكة
    تنظيف منازل بجدة تنظيف منازل بجدة
    تنظيف منازل بالمدينة المنورة تنظيف منازل بالمدينة المنورة

    ReplyDelete
  6. Pakistan has made its Kirkri by and by on the issue of Jammu and Kashmir. While supporting Pakistan, China attempted to get the help of different nations in the United Nations Security Council (UNSC) on the Kashmir issue, yet the issue didn't appear. On this bombed endeavor of China, different nations said that Kashmir is a respective issue of India and Pakistan.
    Read more - https://www.dainiktimes.com/

    ReplyDelete
  7. Mechanical Sewing Machines–These are residential sewing machines with key highlights and less fasten designs. Mechanical sewing machines are the most famous and modest machines you'll run over. Source - The Daily Buzz

    ReplyDelete
  8. The number 1 online football betting website UFABET is the most popular service in Thailand. Official, online sports betting and all types of casinos. The entrance to UFABET can be played through the website or on the mobile phone. Iphone (iPhone) or Android (Android), which has a safe and professional, reliable and With standards to support Online football betting Through the internet and online casinos that have a variety of games to choose from to play.

    ReplyDelete
  9. Ever thought of getting financially free in your life?
    .
    There are so many options available in the market which will promise you to become financially free. However, they aren't ethical ways to get to the destination.
    .
    Well, you can get financially free in your life by playing games on UFABET and win big prizes. Visit the link and register and start gambling on your favorite sports like Football, Volleyball, and more.

    ReplyDelete
  10. Greeting someone with love is one of the meanest task specially in this hard-paced life. However, you can still do that by giving your special ones a reason to smile and love.

    Well, you can greet people with various methods and one such method is by celebrating your special day with a celebrity. We all have celebrity crushes and therefore you get connected to your favourite celebrity.

    You can follow this guide on how you celebrate with your celebrity. https://greetingly.home.blog/2020/06/30/how-celebrate-your-special-day-with-a-celebrity/

    ReplyDelete
  11. Are you looking for cheap RDP? Well, you can now get monthly dedicated server hosting with DigiRDP, without any long-term commitment.

    DigiRDP offers you thousands of servers in the stock ready for your business to sign-up. Choose the instant dedicated server and signup for your business.

    Get all the essentials features Starting at as low as $10/mo and choose your operating system with DigiRDP.

    ReplyDelete
  12. Abaadd Developers Private Limited opened its doors in 2007. They provide Eco-friendly display of e-waste, Data destruction, asset recovery, reporting & certifications as well as health & safety.

    Since 2007, the Abaadd Developers started their e-recycle services and soon became popular and expanded into the end to end waste management service industry

    ReplyDelete
  13. Are you looking for Snapchat for PC?

    Snapchat is one of the popular social media application present among all teenagers and youngsters.
    .
    Now, you can download with the help of emulator. Here are some of the tips and tricks on Snapchat https://www.syncetech.com/snapchat-for-pc/

    ReplyDelete
  14. Are you looking for Expressvpn Black Friday sale 2020 Deals?
    .
    Well, VPN or Virtual Private Network lets you do internet surfing which is not tracked by your Internet Service Provider or ISP.
    .
    Now, VPN’s like ExpressVPN, TunnelBear, NordVPN are some of the best in business. However, the price of these services is quite expensive. So, here is the Black Friday VPN Deals 2020 for you.

    ReplyDelete