0

I am a total beginner so pardon me If I do no ask question properly. So, I have one ASP.Net MVC - 5 project. So whenever I make any change I run it on localhost and then after testing I publish it to live server. Now my manager wants me to show on website the version of website I am running. I mean is there some version or build number or something that gets changed for every time we publish the web site. I mean do I need to right click on property on the project and fetch some version number or dll number or something which changes on every publish. please guide me a little here. I don't know how to ask this question properly.

As of now I can see the Date of my website:

    System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
    FileInfo fileInfo = new FileInfo(assembly.Location);
    string lastModified = fileInfo.LastWriteTime.Date.ToShortDateString() + " " + fileInfo.LastWriteTime.ToLongTimeString();

Can I fetch other details too regarding the version or something.

I tried two solutions in cshtml

<div>
        @{
            System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
            FileInfo fileInfo = new FileInfo(assembly.Location);
            string lastModified = fileInfo.LastWriteTime.Date.ToShortDateString() + " " + fileInfo.LastWriteTime.ToLongTimeString();
            var e = @ViewContext.Controller.GetType().Assembly.GetName().Version;
            string version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
         }

</div>

e returns 10.999.999.999

version returns 0.0.0.0

Please guide me.

EDIT:

STEP 1:

So I changed the build number from property tab to 10.999.7.9

Now it changed my assembly.cs to

[assembly: AssemblyVersion("10.999.7.9")]

[assembly: AssemblyFileVersion("10.999.999.999")]

Step 2: AutoIncrement

Now the SO LINK suggests to comment out below two lines

[assembly: AssemblyVersion("10.999.7.9")]
[assembly: AssemblyFileVersion("10.999.999.999")]

and replace with

[assembly: AssemblyVersion("1.0.*")]

So, I am confused here. Shall I comment both the lines. But it will take out the effect of [assembly: AssemblyVersion("10.999.7.9")]

Community
  • 1
  • 1
Unbreakable
  • 7,776
  • 24
  • 90
  • 171
  • You mean this? You can use this in your View. `@ViewContext.Controller.GetType().Assembly.GetName().Version` – Kevin Maxwell Jan 16 '17 at 18:56
  • Does this change on every publish to live server? – Unbreakable Jan 16 '17 at 18:57
  • or it's same for once and all. – Unbreakable Jan 16 '17 at 18:57
  • Yes, it does on my application. You can test and see. – Kevin Maxwell Jan 16 '17 at 18:59
  • I will test and will get back to you. Thank you. :) – Unbreakable Jan 16 '17 at 18:59
  • I have added one edit too. – Unbreakable Jan 16 '17 at 19:02
  • To get the date: `@String.Format( "{0:dddd, MMMM d, yyyy HH:mm:ss}", File.GetLastWriteTime(ViewContext.Controller.GetType().Assembly.Location))` – Kevin Maxwell Jan 16 '17 at 19:03
  • Nevermind, I just thought that input will be helpful. I will try your solution. – Unbreakable Jan 16 '17 at 19:04
  • I am already getting date by the code snippet. I just added it coz I thought it might be relavant to my question – Unbreakable Jan 16 '17 at 19:05
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/133297/discussion-between-unbreakable-and-kevin-maxwell). – Unbreakable Jan 16 '17 at 19:14
  • Possible duplicate of [Print Version Number in ASP.NET MVC 4 app](http://stackoverflow.com/questions/12694267/print-version-number-in-asp-net-mvc-4-app) – NightOwl888 Jan 16 '17 at 20:01
  • @NightOwl888 : In my cshtml file I did this `var e = @ViewContext.Controller.GetType().Assembly.GetName().Version;` I got output as 10.999.999.999. Is this it? It should be some other number I guess right?! Am i not fetching correctly? – Unbreakable Jan 16 '17 at 20:20
  • @NightOwl888: I tried the solution given in the question you marked. But it did not work for me. Can you kindly reconsider the duplicate tag. – Unbreakable Jan 16 '17 at 20:35
  • 1
    You have to set up your build process to include a version (see http://stackoverflow.com/q/3975601/215552) in order for the version number to be set. To have it update on every build, see http://stackoverflow.com/q/356543/215552. Then you'll get something sensible in your version string. – Heretic Monkey Jan 16 '17 at 20:43
  • @MikeMcCaughan: I followed the first link and tried the second answer of that link as it was more beginner type answer. So I just made("10.999.7.9"). So, I am all set right. Now I can go to step 2 right? – Unbreakable Jan 16 '17 at 20:59
  • please guide me. So all I need to do is follow second link that you have mentioned and try to auto increment it right? – Unbreakable Jan 16 '17 at 20:59
  • Does the version number you made show up on your page? If so, then yes, go to the second link. – Heretic Monkey Jan 16 '17 at 21:01
  • yes. it shows.. :) I will do 2nd step now and wil get back to you – Unbreakable Jan 16 '17 at 21:02
  • @MikeMcCaughan: I have edited the question. Can you please see once. I am beginner and trying so I get stuck. – Unbreakable Jan 16 '17 at 21:09
  • confused with step 2 – Unbreakable Jan 16 '17 at 21:10
  • Well, only you can answer the question of how you want your build numbers to increment... The accepted answer on that second link says that it will increment the numbers at the asterisk. So if you want the version number to start at 10.999.0.0, make the version number in the attribute "10.999.*"... This question has already ballooned from the original "get version onto page" question it started with, so I'm bailing at this point. If you have further questions about incrementing the version number, please ask a new question. – Heretic Monkey Jan 16 '17 at 21:14

2 Answers2

0

I dont have enough rep to comment, hence creating it as a reply

I am explaining with manual way:

Every time when you update your code - try updating the AssemblyVersion and AssemblyFileVersion in Properties\AssemblyInfo.cs of your project AssemblyInfo

In your controller, have a method that returns the Executing assembly`s version number like below (here I return it as HttpResponseMessage)

var version = (Assembly.GetExecutingAssembly().GetName().Version.ToString());
        return new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(version) };

Hope this helps.

ManiVI
  • 556
  • 1
  • 5
  • 18
0

Can you try this -:

    public ActionResult Index()
    {
        var assemblyversion = this.GetType().Assembly.GetName().Version;            
        ViewBag.VersionNumber = assemblyversion;
        //Pass the viewbag to the view
        return View();
    }
Anup
  • 1
  • 1