在剃刀文件(Blazor)中显示生成日期



我有一个简单的Blazor WASM应用程序。出于调试目的,我想向用户显示构建日期(和时间(。只需在页脚的左下角插入字符串。。。

显然,我想自动做到这一点。在哪里、何时以及如何存储要在剃刀文件中显示的日期时间?

出于调试目的,您可以使用AssemblyTitle(或您喜欢的任何其他属性(

首先,您将其添加到您的csproj文件中

<PropertyGroup>
<AssemblyTitle Condition="'$(Configuration)' == 'debug'">My Assembly $([System.DateTime]::Now)</AssemblyTitle>
</PropertyGroup>

然后在Blazor代码中(MainLayout似乎是一个不错的选择(,您可以提取值并显示它:

<div class="info-panel">@BuildInfo</div>
@code {
string BuildInfo;
#if DEBUG
protected override void OnInitialized()
{
Assembly curAssembly = typeof(Program).Assembly;
BuildInfo = $"{curAssembly.GetCustomAttributes(false).OfType<AssemblyTitleAttribute>().FirstOrDefault().Title}";
}
#endif
}

如果你不喜欢使用现有属性的想法,你可以创建一个自定义属性,但这对我来说似乎太过分了

您可以在以下问题的答案中找到有用的信息。

ASP。NET-在屏幕底部显示应用程序构建日期/信息

最新更新