Using TagHelpers

When running with a Debug build pages are loaded into individual assemblies that change every time you save the file (especially when using Browser Link). When running with a Release build pages are loaded into a PrecompiledViews assembly. These assmblies only have partial version information.

When using a TagHelper in an external library calling GetType() will return the external assembly rather than the assmbly running the web site.

Using TagHelpers locally

Using old version of version TagHelper: 2.0.0.0

Using updated version of version TagHelper: 2.0.0-master.20180324.3

Using TagHelpers in an external library

Using old version of version TagHelper: 1.0.0.0

Using updated version of version TagHelper and passing assembly class: 2.0.0-master.20180324.3

Using updated version of version TagHelper and passing assembly class and AssemblyVersion type: 2.0.0.0

Old version TagHelper


        public override void Process(TagHelperContext context, TagHelperOutput output)
        {
            output.TagName = "span";
            output.Attributes.Add("class", "text-muted");
            var versionInfo = GetType().Assembly.GetName().Version;
            output.Content.Append(versionInfo.ToString());
        }

    

New version TagHelper


        public enum VersionType
        {
            FileVersion = 1,
            ProductVersion = 2,
            InformationalVersion = 2,
            AssemblyVersion = 3
        }

        [HtmlAttributeName("assembly")]
        public System.Type AssemblyType { get; set; }

        [HtmlAttributeName("type")]
        public VersionType Type { get; set; } = VersionType.ProductVersion;
 
        public async override Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
        {
            string versionString = "";
            output.TagName = "span";
            output.TagMode = TagMode.StartTagAndEndTag;
            var childContent = await output.GetChildContentAsync();
            output.Content.AppendHtml(childContent);
            if (AssemblyType == null)
            {
                AssemblyType = GetType();
            }
            switch (Type)
            {
                case VersionType.FileVersion:
                    versionString = AssemblyType
                        .GetTypeInfo().Assembly
                        .GetCustomAttribute()?
                        .Version ?? "";
                    break;
                case VersionType.ProductVersion:    // also covers VersionType.InformationalVersion
                    versionString = AssemblyType
                        .GetTypeInfo().Assembly
                        .GetCustomAttribute()?
                        .InformationalVersion ?? "";
                    break;
                case VersionType.AssemblyVersion:
                    versionString = AssemblyType
                        .Assembly.GetName().Version.ToString();
                    break;
                default:
                    break;
            }
            output.Content.Append(versionString);

            await base.ProcessAsync(context, output);
            return;
        }