简体   繁体   中英

How to know the Web Site version in ASP.net MVC

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")]

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.

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();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM