简体   繁体   English

如何知道ASP.net MVC中的网站版本

[英]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. 因此,我有一个ASP.Net MVC-5项目。 So whenever I make any change I run it on localhost and then after testing I publish it to live server. 因此,每当进行任何更改时,我都在localhost上运行它,然后在测试后将其发布到实时服务器上。 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. 我的意思是我需要右键单击项目的属性,并获取一些版本号或dll号,或在每次发布时都会更改的内容。 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 我在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 e返回10.999.999.999

version returns 0.0.0.0 版本返回0.0.0.0

Please guide me. 请指导我。

EDIT: 编辑:

STEP 1: 第1步:

So I changed the build number from property tab to 10.999.7.9 因此,我将内部版本号从“ 属性”选项卡更改为10.999.7.9

Now it changed my assembly.cs to 现在将我的assembly.cs更改为

[assembly: AssemblyVersion("10.999.7.9")]

[assembly: AssemblyFileVersion("10.999.999.999")]

Step 2: AutoIncrement 第2步:自动递增

Now the SO LINK suggests to comment out below two lines 现在, SO LINK建议将以下两行注释掉

[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")] 但是它将消除[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 每次更新代码时-尝试更新项目AssemblyInfo的 Properties \\ AssemblyInfo.cs中的AssemblyVersion和AssemblyFileVersion

In your controller, have a method that returns the Executing assembly`s version number like below (here I return it as HttpResponseMessage) 在您的控制器中,有一个方法可以返回执行程序集的版本号,如下所示(这里我以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();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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