C# 在 ASP.NET MVC 4 应用程序中打印版本号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12694267/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Print Version Number in ASP.NET MVC 4 app
提问by JQuery Mobile
I have an ASP.NET MVC 4 application. Currently, I am setting the version of the application in the project properties under the "Application" tab. From here, I click the "Assembly Information..." button. Once there, I have entered "1 0 0 *" in the "Assembly version" field.
我有一个 ASP.NET MVC 4 应用程序。目前,我正在“应用程序”选项卡下的项目属性中设置应用程序的版本。从这里,我单击“装配信息...”按钮。在那里,我在“程序集版本”字段中输入了“1 0 0 *”。
My question is, how do I show this value on my web page? Currently, I am trying the following
我的问题是,如何在我的网页上显示此值?目前,我正在尝试以下
@System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString()
Unfortunately, it's always printing "0.0.0.0". Realistically, I'd like to have it print 1.0.0.xyz. I would also like to print the date/time when the last build occurred. However, I have no idea how to do that.
不幸的是,它总是打印“0.0.0.0”。实际上,我想让它打印 1.0.0.xyz。我还想打印上次构建发生的日期/时间。但是,我不知道该怎么做。
What am I doing wrong?
我究竟做错了什么?
采纳答案by Darin Dimitrov
To print the version number of the assembly in which was defined the controller that rendered this view:
要打印在其中定义呈现此视图的控制器的程序集的版本号:
@ViewContext.Controller.GetType().Assembly.GetName().Version
and for the assembly date:
和组装日期:
@File.GetCreationTime(ViewContext.Controller.GetType().Assembly.Location)
回答by Ed Chapel
Your assembly version may be set using the AssemblyFileVersionAttribute, which must be accessed specifically.
您的程序集版本可以使用 设置AssemblyFileVersionAttribute,必须专门访问。
AssemblyFileVersionAttribute attr = typeof(MyController).Assembly.GetCustomAttributes(typeof(AssemblyFileVersionAttribute), true).OfType<AssemblyFileVersionAttribute>().FirstOrDefault();
if (attr != null)
{
return attr.Version;
}
The MvcDiagnostics Nuget packagemakes this simple.
该MvcDiagnostics NuGet包使得这个简单的。
回答by chridam
This prints the current version number as outlined in your AssemblyInfo.csfile for printing in an ASP.NET MVC view:
这将打印AssemblyInfo.cs文件中概述的当前版本号,以便在 ASP.NET MVC 视图中打印:
@(typeof(MyController).Assembly.GetName().Version.ToString())
Replacing MyControllerof course with your appropriate MVC controller name.
MyController当然替换为您适当的 MVC 控制器名称。
回答by frennky
I usually make HtmlHelperextension for this purpose. Something like this:
我通常HtmlHelper为此目的进行扩展。像这样的东西:
public static class HtmlHelperExtensions
{
public static IHtmlString AssemblyVersion(this HtmlHelper helper)
{
var version = Assembly.GetExecutingAssembly().GetName().Version.ToString();
return MvcHtmlString.Create(version);
}
}
And than inside view you just call:
而不是内部视图,您只需调用:
@Html.AssemblyVersion()
回答by thanassis
In case you are publishing your application on a production server, I would recommend using something like
如果您在生产服务器上发布应用程序,我建议您使用类似
@String.Format(
"{0:dddd, MMMM d, yyyy HH:mm:ss}",
File.GetLastWriteTime(ViewContext.Controller.GetType().Assembly.Location))
for retrieving the date.
用于检索日期。
This will print the actual publish date since File.GetCreationTime()will give you the date the actual assembly dll was firstcopied on the server.
这将打印实际发布日期,因为File.GetCreationTime()它将为您提供实际程序集 dll首次复制到服务器上的日期。
回答by lukyer
Working solution for correct date of modification after deploy:
部署后正确修改日期的工作解决方案:
@File.GetLastWriteTime(@System.Reflection.Assembly.GetExecutingAssembly().Location)
Can be used in MVC5 too.
也可以在MVC5中使用。
回答by CBarr
If you need this in a Razor view, this works for me. It needed the System.IO.*prefix to work for me
如果您在 Razor 视图中需要这个,这对我有用。它需要System.IO.*前缀才能为我工作
<!--
Version @System.Diagnostics.FileVersionInfo.GetVersionInfo(typeof(YourNamespace.Program).Assembly.Location).ProductVersion
Last deployed on @System.IO.File.GetCreationTime(typeof(YourNamespace.Program).Assembly.Location)
-->
Outputs the following:
输出以下内容:
<!--
Version 1.0.0
Last deployed on 04/19/2019 1:36:24 PM
-->

