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.
Made a slight modification to your code by putting it into the constructor of a Lazy object, like so.
ReplyDeleteprivate 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.
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
ReplyDeleteExcellent post.I would like to learn more about this topic.Keep sharing.
ReplyDeleteRegards,
DOTNET Training in Chennai | DOTNET course in Chennai | DOTNET Training Institute in Chennai
hi there,i got wanted to me.so thanks for that.be helpful
ReplyDeletedot net training chennai
I simply wanted to write down a quick word to say thanks to you for those wonderful tips and hints you are showing on this site.
ReplyDeletedot net training in chennai | dot net training institute in chennai | dot net course in chennai | .NET Training Center in Chennai
| Best .NET Course in Chennai with placements
dorian rossini Its your chance now
ReplyDeleteFashion Hombre follow this blog
ReplyDeleteMechanical 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
ReplyDeleteEver thought of getting financially free in your life?
ReplyDelete.
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.
Excellent post.
ReplyDeleteIELTS Coaching in chennai
German Classes in Chennai
GRE Coaching Classes in Chennai
TOEFL Coaching in Chennai
spoken english classes in chennai | Communication training
Are you looking for cheap RDP? Well, you can now get monthly dedicated server hosting with DigiRDP, without any long-term commitment.
ReplyDeleteDigiRDP 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.
Are you looking for Snapchat for PC?
ReplyDeleteSnapchat 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/
I am professional Gamer working for Foriza Horizon as a Senior Consultant. i love to play games. Here is the best PC for games.
ReplyDeletehttps://www.newmeaccelerator.com/best-atx-case/
Are you looking for the
ReplyDeleteBest Noise Isolating Headphones.
well,headphones under 150 can be tricky because of the numerous options available. We aim to help you by suggesting the best wireless headphones under 150.
https://techno-stuff.com/best-noise-isolating-headphones-under-100/
I am a Interior Designer by Profession. I love to listen to music. Here is my Blog about
ReplyDeleteBuying Guide for Soundbars under 300.
It help to make good advancements in your business that helps you to feel the ultimate confidence knowing that your employees would give you the best performance. It is beneficial especially for new teams who want to do the job well. This gives them the inspiration to carry out the activities efficiently. Salesforce interview questions and answers
ReplyDeleteLooking for the Best Gaming Monitor under 20000?
ReplyDelete.
Well, if you have a high budget and looking for the best gaming monitor you have plenty of options to look and there are bunch of things which you need to keep in mind.
.
So, if you’re a newbie or even a experience user chances are your getting confused. Therefore, we are here to help you with our Top 13 Picks for the Best Monitor under 20000.
Đặt vé tại Aivivu, tham khảo
ReplyDeletevé máy bay đi Mỹ giá bao nhiêu
gia ve tu my ve vietnam
thông tin chuyến bay từ canada về việt nam
dat ve may bay tu han quoc ve viet nam
I WAS SEARCHING FOR THIS KINDA COMPLETE INFORMATION!
ReplyDeleteTHANKS!
ELLA JAMES
Can I simply just say what a comfort to discover an individual who truly understands what they're talking about on the internet. You actually understand how to bring an issue to light and make it important. More people should read this and understand this side of your story. I was surprised you are not more popular since you most certainly have the gift.
ReplyDeleteVisit On: Buy Instagram Story Views
This comment has been removed by the author.
ReplyDeletebest 12 inch subwoofer under 200
ReplyDeleteIf you are a successful residential securityentrepreneur, celebrity, politician, or anyone with immense riches, your children are at risk, and they can become victims of kidnap at any time.
ReplyDeleteCoorg Tour
ReplyDeleteMysore Tour
Kodaikanal
If you are learning how to sew, then here are some good resources for you. You might check them out-
ReplyDeletesinger sewing machine
how to repair sewing machine
how to thread a sewing machine
brother se625
brother xr3774
Your substance material is nothing prompt of throbbing in bunches of ways. I guess this is alluring and eye-start texture. much obliged accurately a huge amount for stressing generally your substance material and your perusers. Happy Friday Quotes For Work
ReplyDeleteGreat post. keep sharing such a worthy information.
ReplyDeleteGerman Classes in Chennai
Great post. keep sharing such a worthy information.
ReplyDeleteAndroid Course in Bangalore
I have to agree with everything in this post. Thanks for useful sharing information.
ReplyDeleteinterview questions on cyber security
cyber security interview questions pdf
cyber security interview questions for freshers
cyber security interview questions for experienced
Mechanical Project in Coimbatore
ReplyDeleteMechanical Autocad Training
Autocad Mechanical Course
Autocad Online Course for Mechanical
Autocad Course for Mechanical Engineering
Cadd Center Courses for Mechanical
Autocad for Mechanical Engineering Courses
Mechanical Cad Training
Cad Course for Mechanical Engineer
Cadd Center Courses for Mechanical Fees
Nice Blog
ReplyDeleteMechanical Project in Coimbatore
Mechanical Autocad Training
Autocad Mechanical Course
Autocad Online Course for Mechanical
Autocad Course for Mechanical Engineering
Cadd Center Courses for Mechanical
Autocad for Mechanical Engineering Courses
Mechanical Cad Training
Cad Course for Mechanical Engineer
Cadd Center Courses for Mechanical Fees
Mechanical Project in Coimbatore
ReplyDeleteMechanical Autocad Training
Autocad Mechanical Course
Autocad Online Course for Mechanical
Autocad Course for Mechanical Engineering
Cadd Center Courses for Mechanical
Autocad for Mechanical Engineering Courses
Mechanical Cad Training
Cad Course for Mechanical Engineer
Cadd Center Courses for Mechanical Fees