Created
June 8, 2021 22:41
-
-
Save tno2007/9381e93ee58e4ed895857abcc640a30c to your computer and use it in GitHub Desktop.
Sirefinity Async Mvc Script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Linq; | |
using System.Web.Mvc; | |
using Telerik.Sitefinity.Frontend.Mvc.Helpers; | |
using Telerik.Sitefinity.Modules.Pages; | |
namespace SitefinityWebApp.Custom | |
{ | |
public static class SfCustomHtmlHelpers | |
{ | |
public static MvcHtmlString ScriptAsync(this HtmlHelper helper, ScriptRef scriptReference) | |
{ | |
var markup = helper.Script(scriptReference, null, false, true); | |
//markup = new MvcHtmlString(markup.ToHtmlString().Replace("src", "defer src")); | |
var doc = new System.Xml.XmlDocument(); | |
doc.LoadXml(markup.ToHtmlString()); | |
var tagBuilder = new TagBuilder("script"); | |
if (doc.DocumentElement == null) return new MvcHtmlString(tagBuilder.ToString(TagRenderMode.EndTag)); | |
tagBuilder.MergeAttributes( | |
doc.DocumentElement.Attributes | |
.Cast<System.Xml.XmlAttribute>() | |
.ToDictionary(x => x.Name, x => x.Value)); | |
tagBuilder.Attributes["async"] = ""; | |
tagBuilder.InnerHtml = doc.DocumentElement.InnerXml; | |
return new MvcHtmlString(tagBuilder.ToString(TagRenderMode.EndTag)); | |
} | |
public static MvcHtmlString ScriptAsync(this HtmlHelper helper, string scriptPath) | |
{ | |
var markup = helper.Script(scriptPath, null, false, true); | |
var doc = new System.Xml.XmlDocument(); | |
doc.LoadXml(markup.ToHtmlString()); | |
var tagBuilder = new TagBuilder("script"); | |
if (doc.DocumentElement == null) return new MvcHtmlString(tagBuilder.ToString(TagRenderMode.EndTag)); | |
tagBuilder.MergeAttributes( | |
doc.DocumentElement.Attributes | |
.Cast<System.Xml.XmlAttribute>() | |
.ToDictionary(x => x.Name, x => x.Value)); | |
tagBuilder.Attributes["async"] = ""; | |
tagBuilder.InnerHtml = doc.DocumentElement.InnerXml; | |
return new MvcHtmlString(tagBuilder.ToString(TagRenderMode.EndTag)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment