Пример #1
0
        /// <summary>
        /// Render template using Razor engine
        /// </summary>
        /// <param name="template"></param>
        /// <param name="model"></param>
        /// <param name="viewBag"> </param>
        /// <param name="cacheName"></param>
        /// <param name="resolveType"></param>
        /// <returns></returns>
        public static string CompileAndRun(string template, dynamic model, DynamicViewBag viewBag = null, string cacheName = "", ResolveType resolveType = ResolveType.Global)
        {
            Type type = model.GetType();

            // Add template to cache if not existed
            var key = TryCompileAndAddTemplate(template, cacheName, type, resolveType);

            // Parse the template
            return(RazorEngineServiceExtensions.Run(Engine.Razor, key, type, model, viewBag));
        }
        protected virtual void RenderModule(HtmlTextWriter writer, object jsonData)
        {
            string str  = "TemplateCacheKey-" + this.ID;
            string key  = "TemplateFileCacheKey-" + this.ID;
            string str3 = "";
            string str4 = HttpContext.Current.Cache[key] as string;

            if (string.IsNullOrEmpty(str4) || (str4.Length == 0))
            {
                string path = HttpContext.Current.Request.MapPath(this.TemplateFile);
                str4 = File.ReadAllText(path);
                str3 = RazorEngineServiceExtensions.RunCompile(Engine.Razor, new LoadedTemplateSource(str4, null), str, null, jsonData, null);
                HttpContext.Current.Cache.Insert(key, str4, new CacheDependency(path), DateTime.MaxValue, TimeSpan.Zero, CacheItemPriority.AboveNormal, null);
                writer.Write(str3);
            }
            else
            {
                str3 = RazorEngineServiceExtensions.IsTemplateCached(Engine.Razor, str, null) ? RazorEngineServiceExtensions.Run(Engine.Razor, str, null, jsonData, null) : RazorEngineServiceExtensions.RunCompile(Engine.Razor, new LoadedTemplateSource(str4, null), str, null, jsonData, null);
                writer.Write(str3);
            }
        }