Unbreakable
Unbreakable

Reputation: 8084

How to display version number and auto increment it in ASP.Net MVC - 5 Application

I am working on ASP.Net MVC web application. So, I make changes in localhost and then run it and test it. Post which I check in my code. From the main server TEAMCITY builds the project and changes goes live. I am a beginner so I don't know how all this takes place. Also, my manager wants me to show the version number on the website footer. So, I looked into this link Add Version Number and made below changes.

cshtml file

     var version = @ViewContext.Controller.GetType().Assembly.GetName().Version;
     <p>Version @version</p>

     //and in AssemblyInfo.cs file I added
[assembly: AssemblyVersion("10.999.7.9")]
[assembly: AssemblyFileVersion("10.999.999.999")]

Now in my local host all I see is 10.999.7.9

Then I found one link which said I need to use "*" to auto increment the version number. this link Increment the Version Number

But now my manager tells me to revert all the changes in AssemblyInfo.cs file and keep it to original which was

[assembly: AssemblyVersion("10.999.999.999")]

[assembly: AssemblyFileVersion("10.999.999.999")]

Final Changes that I am checking IN

[assembly: AssemblyVersion("10.999.999.999")] // BY DEFAULT
[assembly: AssemblyFileVersion("10.999.999.999")]  // BY DEFAULT

and leave the change of cshtml file intact

cshtml

 var version = @ViewContext.Controller.GetType().Assembly.GetName().Version;
         <p>Version @version</p>

So is that it? Apparently, team city will overwrite the AssemblyInfo.cs information and will update the version number. I am totally confused here. I mean don't I need to use * to auto-increment the version number. Also, how the team city overwrites the assembly.cs file so that below code will have the right version number.

var version = @ViewContext.Controller.GetType().Assembly.GetName().Version;

Am I missing something or my manager has missed some details and he is wrong. And to get the correct version number and auto increment it do I need to make changes in Assembly.cs file too and check in assemblyInfo.cs file too? Please guide me.

Upvotes: 4

Views: 6834

Answers (1)

Fran
Fran

Reputation: 6520

You want to setup a build feature called Assembly Patching in TeamCity.

Here's a relevant link

TeamCity will replace the assembly version during the build process and revert it once the build is complete, but the compiled version will be whatever TeamCity says it is.

Then in my Global.asax I've got this code in Application_Start

        Version version = Assembly.GetExecutingAssembly().GetName().Version;
        Application["Version"] = String.Format("{0}.{1}", version.Major, version.Minor);

Then in my _Layout.cshtml I have

        <footer>
            &copy; @DateTime.Now.Year
            Version: @HttpContext.Current.Application["Version"]
        </footer>

Upvotes: 6

Related Questions