예제 #1
0
		protected virtual Task<Stream> Render (CommonViewModel model, string page, string clientName = null)
		{
			var file = string.Format ("~/Content/{0}.cshtml", page);
			rm.RegisterTemplate (file, LoadTemplate (Path.Combine ("~", "Content", string.Format ("{0}.cshtml", page))));
			ITemplate template = rm.ExecuteUrl (file, model);
			return Task.FromResult ((Stream)new MemoryStream (Encoding.UTF8.GetBytes (template.Result)));
		}
예제 #2
0
 protected virtual Task<System.IO.Stream> Render(CommonViewModel model, string page, string clientName = null)
 {
     //Just hardcoded for now
     bool usingRazor = true;
     
     //swap between razor and html
     string html = usingRazor ? LoadRazor(page, model) : LoadHtml(page, model, clientName);
     return Task.FromResult(StringToStream(html));
 }
        protected override async Task<Stream> Render(CommonViewModel model, string page, IEnumerable<string> stylesheets, IEnumerable<string> scripts)
        {
            var data = BuildModelWithLanguageInfo(model, page, stylesheets, scripts);

            string html = await LoadHtmlTemplate(page);
            html = FormatHtmlTemplate(html, data);

            return html.ToStream();
        }
        protected object BuildModelWithLanguageInfo(CommonViewModel model, string page, IEnumerable<string> stylesheets, IEnumerable<string> scripts)
        {
            var data = base.BuildModel(model, page, stylesheets, scripts);
            
            //Extend data object & add language & dir info
            var dynamicData = data.ToDynamic();

            var currentUiCulture = Thread.CurrentThread.CurrentUICulture;
            dynamicData.languageCode = currentUiCulture.TwoLetterISOLanguageName;
            dynamicData.languageTextDirection = currentUiCulture.TextInfo.IsRightToLeft ? "rtl" : "ltr";

            return dynamicData;
        }
예제 #5
0
        protected virtual Task<Stream> Render(CommonViewModel model, string page, string clientName = null)
        {
            var json = Newtonsoft.Json.JsonConvert.SerializeObject(model, Newtonsoft.Json.Formatting.None, new Newtonsoft.Json.JsonSerializerSettings() { ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver() });

            string html = LoadHtml(page);
            html = Replace(html, new {
                siteName = Microsoft.Security.Application.Encoder.HtmlEncode(model.SiteName),
                model = Microsoft.Security.Application.Encoder.HtmlEncode(json),
                clientName = clientName
            });
            
            return Task.FromResult(StringToStream(html));
        }
예제 #6
0
        /// <summary>
        /// Builds the model.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <param name="page">The page.</param>
        /// <param name="stylesheets">The stylesheets.</param>
        /// <param name="scripts">The scripts.</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentNullException">
        /// model
        /// or
        /// stylesheets
        /// or
        /// scripts
        /// </exception>
        protected object BuildModel(CommonViewModel model, string page, IEnumerable<string> stylesheets, IEnumerable<string> scripts)
        {
            if (model == null) throw new ArgumentNullException("model");
            if (stylesheets == null) throw new ArgumentNullException("stylesheets");
            if (scripts == null) throw new ArgumentNullException("scripts");
            
            var applicationPath = new Uri(model.SiteUrl).AbsolutePath;
            if (applicationPath.EndsWith("/")) applicationPath = applicationPath.Substring(0, applicationPath.Length - 1);

            var json = Newtonsoft.Json.JsonConvert.SerializeObject(model, Newtonsoft.Json.Formatting.None, settings);

            var additionalStylesheets = BuildTags("<link href='{0}' rel='stylesheet'>", applicationPath, stylesheets);
            var additionalScripts = BuildTags("<script src='{0}'></script>", applicationPath, scripts);

            return new {
                siteName = Microsoft.Security.Application.Encoder.HtmlEncode(model.SiteName),
                applicationPath,
                model = Microsoft.Security.Application.Encoder.HtmlEncode(json),
                page,
                stylesheets = additionalStylesheets,
                scripts = additionalScripts
            };
        }
예제 #7
0
 /// <summary>
 /// Renders the specified page.
 /// </summary>
 /// <param name="model">The model.</param>
 /// <param name="page">The page.</param>
 /// <returns></returns>
 protected virtual Task<Stream> Render(CommonViewModel model, string page)
 {
     return Render(model, page, config.Stylesheets, config.Scripts);
 }
예제 #8
0
 /// <summary>
 /// Builds the model.
 /// </summary>
 /// <param name="model">The model.</param>
 /// <param name="page">The page.</param>
 /// <param name="stylesheets">The stylesheets.</param>
 /// <param name="scripts">The scripts.</param>
 /// <returns></returns>
 /// <exception cref="System.ArgumentNullException">
 /// model
 /// or
 /// stylesheets
 /// or
 /// scripts
 /// </exception>
 protected object BuildModel(CommonViewModel model, string page, IEnumerable<string> stylesheets, IEnumerable<string> scripts)
 {
     return BuildModelDictionary(model, page, stylesheets, scripts);
 }
예제 #9
0
        /// <summary>
        /// Renders the specified page.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <param name="page">The page.</param>
        /// <param name="stylesheets">The stylesheets.</param>
        /// <param name="scripts">The scripts.</param>
        /// <returns></returns>
        protected virtual async Task<Stream> Render(CommonViewModel model, string page, IEnumerable<string> stylesheets, IEnumerable<string> scripts)
        {
            var data = BuildModelDictionary(model, page, stylesheets, scripts);

            string html = await LoadHtmlTemplate(page);
            html = FormatHtmlTemplate(html, data);

            return html.ToStream();
        }
예제 #10
0
         /// <summary>
        /// Loads the HTML for the authorize response page.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <returns>
        /// Stream for the HTML
        /// </returns>
        public virtual async Task<Stream> AuthorizeResponse(AuthorizeResponseViewModel model)
        {
            var newModel = new CommonViewModel
            {
                SiteName = model.SiteName, SiteUrl = model.SiteUrl
            };

            var scripts = new List<string>();
            scripts.AddRange(config.Scripts ?? Enumerable.Empty<string>());
            scripts.Add("~/assets/app.FormPostResponse.js");

            var data = BuildModelDictionary(newModel, AuthorizeResponseView, config.Stylesheets, scripts);
            data.Add("responseUri", model.ResponseFormUri);
            data.Add("responseFields", model.ResponseFormFields);

            string html = await LoadHtmlTemplate(AuthorizeResponseView);
            html = FormatHtmlTemplate(html, data);

            return html.ToStream();
        }
예제 #11
0
        /// <summary>
        /// Normal load html for flat html files
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        private string LoadHtml(string name, CommonViewModel model, string clientName = null)
        {
            var json = Newtonsoft.Json.JsonConvert.SerializeObject(model, Newtonsoft.Json.Formatting.None, new Newtonsoft.Json.JsonSerializerSettings() { ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver() });

            //Hardcoded for now
            var file = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"CustomView");
            file = Path.Combine(file, name + ".html");
            var html = File.ReadAllText(file);

            html = Replace(html, new
            {
                siteName = SoftwareKobo.Net.WebUtility.HtmlEncode(model.SiteName),
                model = SoftwareKobo.Net.WebUtility.HtmlEncode(json),
                clientName = clientName
            });

            return html;
        }
예제 #12
0
        /// <summary>
        /// Compiling the razor files first, then converting them to a flat html string
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        private string LoadRazor(string name, CommonViewModel model)
        {
            //Hardcoded for now
            var file = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"RazorCustomView");
            file = Path.Combine(file, name + ".cshtml");
            var htmlContent = File.ReadAllText(file);

            var uniqueKey = Guid.NewGuid(); //I think the template keys are meant to be unique
            string razorContent = Engine.Razor.RunCompile(htmlContent, uniqueKey.ToString(), null, model);

            return razorContent;
        }