using System; using Microsoft.AspNetCore.Html; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Razor; using Microsoft.AspNetCore.Mvc.Rendering; using Microsoft.AspNetCore.Mvc.Routing; namespace Infrastructure.Extensions { public static class HtmlHelperExtensions { public static IUrlHelper GetUrlHelper(this IHtmlHelper helper) { if (helper is null) { throw new ArgumentNullException(nameof(helper)); } return new UrlHelperFactory().GetUrlHelper(helper.ViewContext); } public static void Set(this IHtmlHelper helper, string key, string value) { if (helper is null) { throw new ArgumentNullException(nameof(helper)); } helper.ViewContext.HttpContext.Items.Add(key, value); } public static IHtmlContent Get(this IHtmlHelper helper, string key) { if (helper is null) { throw new ArgumentNullException(nameof(helper)); } if (helper.ViewContext.HttpContext.Items.TryGetValue(key, out object value)) { return new HtmlString(value as string); } return HtmlString.Empty; } } }