{{announcement.body}}
{{announcement.title}}

Assembly Version Tag Helper for ASP.NET Core

DZone 's Guide to

Assembly Version Tag Helper for ASP.NET Core

A DZone MVB and ASP.NET Core guru gives a quick tutorial on how to use tag helpers in your app and shows how they can help write DRY code.

· Web Dev Zone ·
Free Resource

I'm writing some web applications where I must show the assembly version in the footer of all the pages. The applications are published online through Azure DevOps release pipelines and I'm using automatic date-based versioning for the assemblies. Here is how to show the application version using assembly the version tag helper.

I have multiple pages where the version is displayed. Besides the layout page, there are also standalone pages, like the login page and error pages. Plus, I have two web applications in the solution. I don't want to keep the version displaying logic in all these pages as that would be 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 the tag helper because it makes the tag helper depend on these specific designs.

Here is how I use the tag helper on pages.

NB! Don't forget to register tag helper in _ViewImports.cshtml file located in the Views folder. If you don't register the tag helper then you don't get any errors because the Razor view engine will treat it like any other HTML tag.

<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>

Wrapping Up

Although assembly version tag helper is not something big or genius, it helps me keep all the version displaying logic in one place and I don't have to duplicate the same code to multiple views.



If you enjoyed this article and want to learn more about ASP.NET, check out this collection of tutorials and articles on all things ASP.NET.

Topics:
web dev ,asp.net core tutorial ,c# tutorial ,web application development ,dry code

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}