Beispiel #1
0
        /// <summary>
        /// 生成静态页(带母版)
        /// </summary>
        /// <returns></returns>
        public bool ToPage(dynamic model)
        {
            CompilerServiceBuilder.SetCompilerServiceFactory(new DefaultCompilerServiceFactory());
            using (var service = new TemplateService())
            {
                string master  = System.IO.File.ReadAllText(MasterUrl, OutputEncoding);
                string content = System.IO.File.ReadAllText(TemplateUrl, OutputEncoding);
                service.Compile(content, model.GetType(), MasterMapper);
                string result = service.Parse(master, model);
                if (File.Exists(SaveUrl))
                {
                    File.Delete(SaveUrl);
                }

                using (FileStream stream = new FileStream(SaveUrl, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
                {
                    Byte[] info = OutputEncoding.GetBytes(result);
                    stream.Write(info, 0, info.Length);
                    stream.Flush();
                    stream.Close();
                    return(true);
                }
            }
            return(false);
        }
Beispiel #2
0
        static void Main(string[] args)
        {
            CompilerServiceBuilder.SetCompilerServiceFactory(new DefaultCompilerServiceFactory());

            using (var service = new TemplateService())
            {
                const string template  = "<h1>Age: @Model.Age</h1>";
                var          expected  = Enumerable.Range(1, 10).Select(i => string.Format("<h1>Age: {0}</h1>", i)).ToList();
                var          templates = Enumerable.Repeat(template, 10).ToList();
                var          models    = Enumerable.Range(1, 10).Select(i => new Person {
                    Age = i
                });

                var results = service.ParseMany(templates, models, null, null, true).ToList();

                for (int i = 0; i < 10; i++)
                {
                    Console.WriteLine(templates[i]);
                    Console.WriteLine(expected[i]);
                    Console.WriteLine(results[i]);
                }
            }

            Console.ReadKey();
        }
Beispiel #3
0
 public RazorFormatter()
 {
     // make sure we load assemblies we plan to reference in the Views
     typeof(Microsoft.CSharp.RuntimeBinder.Binder).Assembly.ToString();
     typeof(System.Web.HttpUtility).Assembly.ToString();
     CompilerServiceBuilder.SetCompilerServiceFactory(new DefaultCompilerServiceFactory());
     SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
     SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/xhtml+xml"));
 }
Beispiel #4
0
 /// <summary>
 /// 生成并保存静态页面(只支持单一模板,不支持多页面嵌套)
 /// </summary>
 /// <param name="templatepath">模板地址</param>
 /// <param name="savepath">保存地址</param>
 /// <param name="encoding">编码格式</param>
 /// <param name="o">数据</param>
 /// <returns></returns>
 public static bool ModelToStaticPage <T>(string templatepath, string savepath, Encoding encoding, T o)
 {
     CompilerServiceBuilder.SetCompilerServiceFactory(new DefaultCompilerServiceFactory());
     using (var service = new TemplateService())
     {
         var    index  = System.IO.File.ReadAllText(templatepath, encoding);
         string result = service.Parse(index, o);
         using (FileStream stream = new FileStream(savepath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
         {
             Byte[] info = encoding.GetBytes(result);
             stream.Write(info, 0, info.Length);
             stream.Flush();
             stream.Close();
             return(true);
         }
     }
 }