Skip to content

Instantly share code, notes, and snippets.

@falafelsoftware
Last active August 29, 2015 14:01

Revisions

  1. falafelsoftware revised this gist May 8, 2014. No changes.
  2. falafelsoftware created this gist May 8, 2014.
    55 changes: 55 additions & 0 deletions gistfile1.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,55 @@
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Web.UI;
    using HtmlAgilityPack;
    using Telerik.Sitefinity.Configuration;

    namespace Sitefinity.Framework.Web.Controls
    {
    /// <summary>
    /// A base class that will be used for the master pages to remove the Telerik Web resource
    /// </summary>
    public class MasterBase : MasterPage
    {
    protected override void Render(HtmlTextWriter writer)
    {
    //Render normally in the backend or if the setting is false
    if (this.IsBackend() || this.IsDesignMode())
    {
    base.Render(writer);
    return;
    }

    var tw = new StringWriter();
    var htw = new HtmlTextWriter(tw);

    base.Render(htw);

    //Get page source
    var pageSource = tw.ToString();

    //Load the html document to use HtmlAgilityPack
    var htmlDoc = new HtmlDocument();
    htmlDoc.LoadHtml(pageSource);

    if (htmlDoc.DocumentNode == null) return;

    //Get the telerik UI web resource scripts
    var bodyNode = htmlDoc.DocumentNode.SelectNodes("//script").Where(s => s.GetAttributeValue("src", string.Empty).Contains(Constants.TELERIK_WEBUI_WEBRESOURCE) || string.IsNullOrEmpty(s.GetAttributeValue("src", string.Empty)));

    foreach (var node in bodyNode)
    {
    //Delete the scripts if there is a match
    node.ParentNode.RemoveChild(node);
    }

    pageSource = htmlDoc.DocumentNode.OuterHtml;

    //render the outer html
    writer.Write(pageSource);
    }
    }
    }