Skinning page from html

From Logic Wiki
Jump to: navigation, search


Web forms usage

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Optevia.Portal.OPSE.MasterPages.Customer
{
    /// <summary>
    /// The base master page for all site master pages
    /// </summary>
    public partial class Base : CustomerPortalMasterPage
    {
        /// <summary>
        /// Executes on page load
        /// </summary>
        /// <param name="sender">The control that invoked the event</param>
        /// <param name="e">The EventArgs object that describes the context of the event</param>
        protected void Page_Load(object sender, EventArgs e)
        {
              //var pageId = "/Pages/MyAccount.aspx";
              //SkinPage("http://rochdale.gov.uk", pageId);
            var pageId = "/Template.html";
            SkinPage("http://MyAccount-uat", pageId);
        }


        
        public static string HeadSection { get; set; }
        public static string BodySection { get; set; }
        public static string FooterSection { get; set; }
        public static string AlertSection { get; set; }

 
        public static void SkinPage( string domain, string pageId)
        {
            var strPageHTML = "";
            

            try
            {
                strPageHTML = new WebClient().DownloadString(domain + pageId);
            }

            catch (WebException ex)
            {
                if (ex.Status != WebExceptionStatus.ProtocolError)
                {

                }
                if (ex.Response != null)
                {
                    if ((ex.Response.Headers == null ? false : ex.Response.Headers.Count > 0))
                    {

                    }
                }
            }

            domain = "http://rochdale.gov.uk";
            strPageHTML = strPageHTML.Replace("src=\"/site_images", "src=\"" + domain + "/site_images");
            strPageHTML = strPageHTML.Replace("src=\"site_images", "src=\"" + domain + "/site_images");
            strPageHTML = strPageHTML.Replace("src=\"/", "src=\"" + domain + "/");
            strPageHTML = strPageHTML.Replace("src=\"images", "src=\"" + domain + "/Images");
            strPageHTML = strPageHTML.Replace("href=\"/", "href=\"" + domain + "/");
            strPageHTML = strPageHTML.Replace("http://www.rochdale.gov.uk/" + pageId, "http://www.rochdale.gov.uk/");
            strPageHTML = strPageHTML.Replace("<meta http-equiv=\"X-UA-Compatible\" content=\"IE=10\" />", "<meta http-equiv=\"X-UA-Compatible\" content=\"IE=9\" />");
            strPageHTML = strPageHTML.Replace("http://internet.rochdale.gov.uk///s3.amazonaws.com/cc.silktide.com/cookieconsent.latest.min.js", "http://s3.amazonaws.com/cc.silktide.com/cookieconsent.latest.min.js");
            var bytes = Encoding.Default.GetBytes(strPageHTML);
            strPageHTML = Encoding.UTF8.GetString(bytes);
           
            HeadSection = SectionHTML(strPageHTML, "<!-- Header_Template_Start -->", "<!-- Header_Template_End -->").Replace("<head>", "").Replace("</head>", "");
            BodySection = SectionHTML(strPageHTML, "<!-- BodyTop_Template_Start -->", "<!-- BodyTop_Template_End -->");
            AlertSection = SectionHTML(strPageHTML, "<!-- Alert_Start -->", "<!-- Alert_End -->");
            FooterSection = SectionHTML(strPageHTML, "<!-- Footer_Start -->", "<!-- Footer_End -->");
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="htmlContent"></param>
        /// <param name="startTag"></param>
        /// <param name="endTag"></param>
        /// <returns></returns>
        private static string SectionHTML(string htmlContent, string startTag, string endTag)
        {
            var strReturn = "";
            var intSectionStart = htmlContent.IndexOf(startTag);
            if (intSectionStart > -1)
            {
                var intSectionEnd = htmlContent.IndexOf(endTag, intSectionStart);
                strReturn = htmlContent.Substring(intSectionStart + startTag.Length, intSectionEnd - intSectionStart - startTag.Length);
            }
            return strReturn;
        }
    }
}

Different parts in MVC Usage

 public static void SkinPage(Controller controller, string domain, string pageId)
        {
            var strPageHTML = "";
            controller.ViewBag.HeadSection = "";   // <!-- Header_Template_Start --> <!-- Header_Template_End -->

            controller.ViewBag.BodyTop = "";       // <!-- BodyTop_Template_Start --> <!-- BodyTop_Template_End -->

            controller.ViewBag.BodyBottom = "";    // <!-- BodyBottom_Template_Start --> <!-- BodyBottom_Template_End -->

And in controller

 public class HomeController : Controller
    {
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            var pageId = "/Pages/MyAccount.aspx";
            Skinning.SkinPage(this, "http://rochdale.gov.uk", pageId);
            return View();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
    }