Friday, 12 September 2014

Html file call in Master page as contain placeholder -- dynamic usercontrol for all pages as per folder /page

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.IO;

public partial class Html_htmlcall : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            try
            {
                //------------------------find file name with folder name start code-------------

                string filename = Path.GetFileName(HttpContext.Current.Request.Url.AbsolutePath);
                string urlname = HttpContext.Current.Request.Url.AbsolutePath;
                //for my local
               //urlname = urlname.Replace("/NewRnd", "");
                //compete
                string folderpath = urlname.Replace("/" + filename, "");
                if (folderpath.StartsWith("/"))
                {
                    folderpath = folderpath.Remove(0, 1);
                }
                string[] folderlength = folderpath.Split('/');
                string newfolderpath = "";
                if (folderlength.Length > 0)
                {
                    foreach (string s in folderlength)
                    {
                        newfolderpath = newfolderpath + s + "-";
                    }
                }
                if (newfolderpath.Length == 1)
                {
                    newfolderpath = "";
                }

                //---------------------------end code-------------

                //Response.Write(newfolderpath);

                string uriFileExt = Path.GetExtension(HttpContext.Current.Request.Url.AbsolutePath);// HttpContext.Current.Request.Url.AbsoluteUri;
                string uri1 = HttpContext.Current.Request.Url.AbsoluteUri.Replace(uriFileExt, ".html");
                string pagename=Path.GetFileName(HttpContext.Current.Request.Url.AbsolutePath).Replace(uriFileExt, ".html");
                //string uri = "http://10.132.150.2:8313/thankyou.html";
                string uri = "http://" + HttpUtility.UrlDecode(HttpContext.Current.Request.ServerVariables["http_host"] + "/uploads/"+ newfolderpath+ pagename);

               // string uri = "http://" + HttpUtility.UrlDecode(HttpContext.Current.Request.ServerVariables["http_host"] + "/uploads/voting-policy.html");
                HttpWebRequest req = WebRequest.Create(uri) as HttpWebRequest;
                req.KeepAlive = false;
                //req.ContentLength = 0;
                //req.ContentType = "text/json";
                //Stream data = req.GetRequestStream();
                //data.Close();
                string result;
                using (WebResponse resp = req.GetResponse())
                {
                    using (StreamReader reader = new StreamReader(resp.GetResponseStream()))
                    {
                        result = reader.ReadToEnd();
                    }
                }
                htmlprindiv.InnerHtml = result;
                // rtxtResult.AppendText(htmlText);
            }
            catch (Exception ex)
            {
                //rtxtResult.AppendText(ex.Message);
            }
        }
    }
}