using Microsoft.AspNetCore.Html; using Microsoft.AspNetCore.Mvc.Rendering; using Microsoft.AspNetCore.Mvc.ViewFeatures; using Microsoft.AspNetCore.Razor.TagHelpers; namespace TurnstileTag; [HtmlTargetElement("form", Attributes = "cf-turnstile")] public class TurnstileAttributeTagHelper : TagHelper { readonly ITurnstile _turnstile; public TurnstileAttributeTagHelper(ITurnstile turnstile) { _turnstile = turnstile; } /// /// Whether turnstile should be activated. /// public bool? CfTurnstile { get; set; } /// /// Gets the of the executing view. /// [HtmlAttributeNotBound] [ViewContext] public ViewContext? ViewContext { get; set; } public override void Process(TagHelperContext context, TagHelperOutput output) { if (CfTurnstile ?? false) { IHtmlContent content = _turnstile.GenerateTurnstileFormHtml(ViewContext!); if (content != null) { output.PostContent.AppendHtml(content); } } } }