public override string ParseTemplate(TemplatePart template, ParseTemplateContext context)
        {
            int    versionrecordid = template.Record.ContentItemRecord.Versions.Where(x => x.Published).FirstOrDefault().Id;
            string key             = _shellSettings.Name + versionrecordid.ToString();
            // var tr = new RazorTemplateManager();
            var layout          = template.Layout;
            var templateContent = template.Text;
            //var viewBag = context.ViewBag;
            string layoutString = null;

            if (layout != null)
            {
                key         += "_" + template.Layout.Record.ContentItemRecord.Versions.Where(x => x.Published).FirstOrDefault().Id;
                layoutString = layout.Text;
                //  _razorTemplateManager.AddLayout("Layout" + key, layout.Text);
                templateContent = "@{ Layout = \"Layout" + key + "\"; }\r\n" + templateContent;
            }

            try {
                var tmpl = _razorTemplateManager.RunString(key, templateContent, context.Model, (Dictionary <string, object>)context.ViewBag, layoutString);
                return(tmpl);
            }
            catch (Exception ex) {
                Logger.Log(LogLevel.Error, ex, "Failed to parse the {0} Razor template with layout {1}", template.Title, layout != null ? layout.Title : "[none]");
                return(BuildErrorContent(ex, template, layout));
            }
        }
Beispiel #2
0
        private dynamic RazorTransform(string xmlpage, string xsltname, string contentType = "", Dictionary <string, object> dvb = null)
        {
            string output = "";
            string myfile = HostingEnvironment.MapPath("~/") + @"App_Data\Sites\" + _shellSetting.Name + @"\Xslt\" + contentType + xsltname + ".cshtml";

            if (!System.IO.File.Exists(myfile))
            {
                myfile = HostingEnvironment.MapPath("~/") + @"App_Data\Sites\" + _shellSetting.Name + @"\Xslt\" + xsltname + ".cshtml";
            }

            if (System.IO.File.Exists(myfile))
            {
                string   key = myfile;
                DateTime d   = System.IO.File.GetLastWriteTime(myfile);
                key += d.ToShortDateString() + d.ToLongTimeString();
                string mytemplate = File.ReadAllText(myfile);
                string myfile2    = HostingEnvironment.MapPath("~/") + @"App_Data\Sites\common.cshtml";
                if (System.IO.File.Exists(myfile2))
                {
                    mytemplate = File.ReadAllText(myfile2) + mytemplate;

                    // add the date of common.cshtml to the key, to update the file even if only the common has changed and not just the file
                    DateTime d2 = System.IO.File.GetLastWriteTime(myfile2);
                    key += d2.ToShortDateString() + d2.ToLongTimeString();
                }
                if (!string.IsNullOrEmpty(mytemplate))
                {
                    var    docwww = XDocument.Parse(xmlpage);
                    string result = _razorTemplateManager.RunString(key, mytemplate, docwww, dvb, null);
                    output = result.Replace("\r\n", "");
                }
                else
                {
                    output = "";
                }
                while (output.StartsWith("\t"))
                {
                    output = output.Substring(1);
                }

                // Fix per non generare eccezione se output è uguale a ""
                string xml = "<root />";
                if (output != "")
                {
                    xml = RemoveAllNamespaces(output);
                }

                XmlDocument doc = new XmlDocument();
                doc.LoadXml(xml);
                XmlNode newNode = doc.DocumentElement;
                newNode = XmlWithJsonArrayTag(newNode, doc);

                string JsonData = JsonConvert.SerializeXmlNode(newNode);
                JsonData = JsonData.Replace(",\"\"]}", "]}"); // aggiunto perchè nella nuova versione di newtonjson i nodi xml vuoti non vengono piu tradotti in null ma in ""
                JsonData = JsonData.Replace(",{}]}", "]}");

                JsonData = JsonData.Replace("\":lasernumericlasernumeric:\"", "null");
                JsonData = JsonData.Replace("\":lasernumeric", "");
                JsonData = JsonData.Replace("lasernumeric:\"", "");
                JsonData = JsonData.Replace("\":laserbooleanlaserboolean:\"", "null");
                JsonData = JsonData.Replace("\":laserboolean", "");
                JsonData = JsonData.Replace("laserboolean:\"", "");
                JsonData = JsonData.Replace(@"\r\n", "");
                JsonData = JsonData.Replace("\":laserDatelaserDate:\"", "null");
                JsonData = JsonData.Replace("\":laserDate", "\"\\/Date(");
                JsonData = JsonData.Replace("laserDate:\"", ")\\/\"");


                JavaScriptSerializer ser = new JavaScriptSerializer()
                {
                    MaxJsonLength = Int32.MaxValue
                };
                dynamic dynamiccontent_tmp = ser.Deserialize(JsonData, typeof(object));
                dynamic dynamiccontent     = new DynamicJsonObject(dynamiccontent_tmp as IDictionary <string, object>);

                return(dynamiccontent);
            }
            else
            {
                return(XsltTransform(xmlpage, xsltname, contentType));
            }
        }