Skip to content

Commit bc0192f

Browse files
author
Warren Buckley
committed
Adds <our-version> to help display/print out assembly version of Website or given Assembly
1 parent dfb1728 commit bc0192f

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using Microsoft.AspNetCore.Razor.TagHelpers;
2+
using System;
3+
using System.Linq;
4+
using System.Reflection;
5+
6+
namespace Our.Umbraco.TagHelpers
7+
{
8+
/// <summary>
9+
/// Used to print out the current version of the Assembly Name
10+
/// Only Assemblies loaded into the Current AppDomain can be looked up
11+
/// </summary>
12+
[HtmlTargetElement("our-version")]
13+
public class VersionTagHelper : TagHelper
14+
{
15+
/// <summary>
16+
/// Name of assembly to lookup minus the .dll extension
17+
/// 'Our.Umbraco.TagHelpers'
18+
/// </summary>
19+
[HtmlAttributeName("assembly")]
20+
public string AssemblyName { get; set; } = string.Empty;
21+
22+
public override void Process(TagHelperContext context, TagHelperOutput output)
23+
{
24+
output.TagName = "";
25+
26+
// If no Assembly name set on attribute
27+
// Then use the
28+
if (string.IsNullOrEmpty(AssemblyName))
29+
{
30+
var siteAssembly = Assembly.GetEntryAssembly();
31+
var version = siteAssembly?.GetName().Version;
32+
output.Content.SetHtmlContent(version.ToString());
33+
return;
34+
}
35+
36+
var allAssemblies = AppDomain.CurrentDomain.GetAssemblies();
37+
var findAssembly = allAssemblies.SingleOrDefault(x => x.GetName().Name == AssemblyName);
38+
if(findAssembly == null)
39+
{
40+
output.SuppressOutput();
41+
return;
42+
}
43+
output.Content.SetHtmlContent(findAssembly.GetName().Version.ToString());
44+
}
45+
}
46+
}

0 commit comments

Comments
 (0)