To further expand on the work of @Vadim. In .Net 5.0 you can use the AssemblyMetadata tag.
Example Below. This is for a .Net 7.0 Blazor project to put the build date, Git Hash, and Git, branch in the header of the webpage.
Project.csproj
<PropertyGroup>
<!--Determines if compiler should add the attributes to the assembly-->
<GenerateGitMetadata>True</GenerateGitMetadata>
<PropertyGroup>
<Target Name="AddGitMetadaAssemblyAttributes"
BeforeTargets="GetAssemblyAttributes"
Condition=" '$(GenerateGitMetadata)' == 'true' ">
<!--Executes the Git Commands to get the Hash and Branch-->
<Exec Command="git rev-parse --short=8 HEAD" ConsoleToMSBuild="true" StandardOutputImportance="low" IgnoreExitCode="true" Condition=" '$(CommitHash)' == '' ">
<Output TaskParameter="ConsoleOutput" PropertyName="CommitHash" />
</Exec>
<Exec Command="git rev-parse --abbrev-ref HEAD" ConsoleToMSBuild="true" StandardOutputImportance="low" IgnoreExitCode="true" Condition=" '$(CommitBranch)' == '' ">
<Output TaskParameter="ConsoleOutput" PropertyName="CommitBranch" />
</Exec>
<!--Generates the ItemGroup and all AssemblyMetadata Tags-->
<ItemGroup>
<AssemblyMetadata Include="web_BuildTimestamp" Value="$([System.DateTime]::UtcNow.ToString(yyyy-MM-ddTHH:mm:ssK))" />
<AssemblyMetadata Condition=" $(CommitHash) != '' " Include="web_CommitHash" Value="$(CommitHash)" />
<AssemblyMetadata Condition=" $(CommitBranch) != '' " Include="web_CommitBranch" Value="$(CommitBranch)" />
</ItemGroup>
</Target>
Any class can now access the attributes with the following code.
var assemblyAttributes = Assembly.GetExecutingAssembly()
.GetCustomAttributes<AssemblyMetadataAttribute>()
For Example _Host.cshtml (at the top)
@{
string assemblyAttributePrefix = "web_";
var assemblyAttributes = Assembly.GetExecutingAssembly().GetCustomAttributes<AssemblyMetadataAttribute>().Where(x => x.Key.StartsWith(assemblyAttributePrefix));
}
In the Body
@foreach (var attr in assemblyAttributes)
{
<meta name="@attr.Key.Replace(assemblyAttributePrefix,string.Empty)" content="@attr.Value" />
}