Skip to content

Instantly share code, notes, and snippets.

@tno2007
Created June 8, 2021 22:41
Show Gist options
  • Save tno2007/9381e93ee58e4ed895857abcc640a30c to your computer and use it in GitHub Desktop.
Save tno2007/9381e93ee58e4ed895857abcc640a30c to your computer and use it in GitHub Desktop.
Sirefinity Async Mvc Script
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