由于 ASP.net 标记也包含在问题中,我想扩展 Maxim Kornilov 的回答 (https://stackoverflow.com/a/12992813/903783) 以及我如何使用他在 ASP.net 上制作特定于 webapp-build 的 URL 的想法MVC(他的示例使用 ASP/ASP.net WebForms 语法,而不是 MVC 和 Razor Pages 的新 Razor 语法):
1) 在 Global.asax.cs 中添加到 webapp 的主类(称为 MvcApplication)
#region Versioning
public static string Version => typeof(MvcApplication).Assembly.GetName().Version.ToString(); //note: syntax requires C# version >=6
public static DateTime LastUpdated => File.GetLastWriteTime(typeof(MvcApplication).Assembly.Location);
#endregion
someProperty => someReadOnlyExpression 语法只是 someProperty { get { return ... ;} } 自 C# 6 起可能的简写
2) 在其 Content/_Layout.cshtml 文件中,我曾经在页脚上显示构建号和构建日期时间(基于 webapp 的主程序集):
Version @ViewContext.Controller.GetType().Assembly.GetName().Version (@string.Format("{0:yyyy/MM/dd-HH:mm:ss}", @File.GetLastWriteTime(ViewContext.Controller.GetType().Assembly.Location)))
我改成了更简单的:
Version @somewebappname.MvcApplication.Version (@string.Format("{0:yyyy/MM/dd-HH:mm:ss}", somewebappname.MvcApplication.LastUpdated))
3) 它通过 _Layout.cshtml 中的硬编码链接加载 CSS(仍在重构),我将其更改为:
<link href='@Url.Content("~/Content/Site.css?version=" + somewebappname.MvcApplication.Version)' rel="stylesheet" type="text/css" />
因此,如果在网页中单击鼠标右键,他们确实会查看他们看到的源代码:
<link href='/Content/Site.css?version=2.1.5435.22633' rel="stylesheet" type="text/css" />
由于虚拟参数版本,CSS url 是特定于版本的
如果使用随机数,它会在每次页面加载时获取 CSS,这通常是不希望的,特别是如果您已经将新的 webapp 构建而不是单独的页面更改推送到 web 服务器(这样您就可以访问到可以注入 URL 的内部版本号)。
请注意,要实现内部版本号的自动递增,在 Properties/AssemblyInfo.cs 我有(请参阅How to have an auto incrementing version number (Visual Studio)?):
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Revision and Build Numbers
// by using the '*' as shown below:
[assembly: AssemblyVersion("1.0.*")]
//[assembly: AssemblyFileVersion("1.0.*")] //don't use boh AssemblyVersion and AssemblyFileVersion with auto-increment