private static TagBuilder GetStaticElement(VueScriptLocation location, string url) { switch (location) { case VueScriptLocation.Head: var headTag = new TagBuilder("link"); headTag.MergeAttributes(new Dictionary <string, string>() { { "rel", "preload" }, { "as", "script" }, { "href", url } }); return(headTag); case VueScriptLocation.Foot: var bodyTag = new TagBuilder("script"); bodyTag.MergeAttributes(new Dictionary <string, string>() { { "src", url } }); return(bodyTag); default: throw new ArgumentOutOfRangeException(nameof(location), location, null); } }
/// <summary> /// Renders the script tags using the appUrl and vueUrl values in the application configuration file. /// </summary> /// <param name="helper"></param> /// <param name="location">The location of the script tag. Use Head to render preload links, Foot to render the actual script tags.</param> /// <returns></returns> public static HtmlString RenderVueScripts(this HtmlHelper helper, VueScriptLocation location = VueScriptLocation.Foot) { var vueLink = GetStaticElement(location, VueConfig.Settings.VueUrl); var appLink = GetStaticElement(location, VueConfig.Settings.AppUrl); return(new HtmlString($"{vueLink}{appLink}")); }
/// <summary> /// Renders the script tags using the appUrl and vueUrl values in the application configuration file. /// </summary> /// <param name="helper"></param> /// <param name="location">The location of the script tag. Use Head to render preload links, Foot to render the actual script tags.</param> /// <returns></returns> public static HtmlString RenderScriptTags(VueScriptLocation location = VueScriptLocation.Foot) { var vueLink = GetStaticElement(location, VueConfig.Settings.VueUrl); var appLink = GetStaticElement(location, VueConfig.Settings.AppUrl); var result = $"{vueLink}{appLink}"; foreach (var script in VueConfig.Settings.Scripts) { result += GetStaticElement(location, script.Url, !script.NoHash); } return(new HtmlString(result)); }
private static TagBuilder GetStaticElement(VueScriptLocation location, string url, bool cacheBust = false) { if (string.IsNullOrEmpty(url)) { return(null); } if (VueConfig.Settings.CacheBust && cacheBust) { var hash = url.GetFileHash(); if (!string.IsNullOrEmpty(hash)) { if (url.Contains("?")) { url += "&"; } else { url += "?"; } url += hash; } } switch (location) { case VueScriptLocation.Head: var headTag = new TagBuilder("link"); headTag.MergeAttributes(new Dictionary <string, string>() { { "rel", "preload" }, { "as", "script" }, { "href", url } }); return(headTag); case VueScriptLocation.Foot: var bodyTag = new TagBuilder("script"); bodyTag.MergeAttributes(new Dictionary <string, string>() { { "src", url } }); return(bodyTag); default: throw new ArgumentOutOfRangeException(nameof(location), location, null); } }