Assembly version tag helper for ASP.NET Core
I’m writing some web applications where I must show assembly version in footer of all pages. Applications are published online through Azure DevOps release pipelines and I’m using automatic date based versioning for assemblies. Here is how to show application version using assembly version tag helper.
I have multiple pages where version is displayed. Besides layout page there are also standalone pages like login page and error pages. Plus I have two web applications in solution. I don’t want to keep version displaying logic in all these pages as it is code duplication.
Here is the assembly version tag helper I created.
[HtmlTargetElement("AssemblyVersion", TagStructure = TagStructure.NormalOrSelfClosing)]
public class AssemblyVersionTagHelper : TagHelper
{
public override void Process(TagHelperContext context, TagHelperOutput output)
{
output.TagName = "";
output.Content.Append(GetType().Assembly.GetName().Version.ToString());
}
}
Notice that tag helper doesn’t output any HTML markup. I don’t want to deal with HTML differences in tag helper because it makes tag helper depend on these specific designs.
Here is how I use tag helper on pages.
<div class="kt-footer kt-grid__item" id="kt_footer">
<div class="kt-container">
<div class="kt-footer__wrapper">
<div class="kt-footer__copyright">
Version: <AssemblyVersion />
</div>
</div>
</div>
</div>
NB! Don’t forget to register tag helper in _ViewImports.cshtml file located in Views folder. If you don’t register tag helper you don’t get any errors because Razor view engine will treat it like any other HTML tag.
Wrapping up
Although assembly version tag helper is not something big or genius it helps me to keep version displaying logic in one place and I don’t have to duplicate the same code to multiple views.