Esempio n. 1
0
        public static string GenerateCodeWithRazorEngine(string razorTemplatePath, Type modelType)
        {
            //-- Configure the code-generator
            var host = new RazorEngineHost(new RazorEngine.Compilation.CSharp.CSharpRazorCodeLanguage(true), () => new HtmlMarkupParser())
            {
                DefaultBaseTemplateType = typeof(TemplateBase <>),
                DefaultModelType        = modelType,
                DefaultBaseClass        = typeof(TemplateBase <>).FullName,
                DefaultClassName        = "RazorViewGeneratedByRazorEngine",
                DefaultNamespace        = "CompiledRazorTemplates.Dynamic",
                GeneratedClassContext   = new GeneratedClassContext("Execute", "Write", "WriteLiteral",
                                                                    "WriteTo", "WriteLiteralTo",
                                                                    "RazorEngine.Templating.TemplateWriter",
                                                                    "DefineSection")
                {
                    ResolveUrlMethodName = "ResolveUrl"
                }
            };

            //-- Parse the template into a CodeDOM graph (see http://msdn.microsoft.com/en-us/library/y2k85ax6(v=vs.110).aspx for an overview of what CodeDOM is)
            var engine = new RazorTemplateEngine(host);
            GeneratorResults result;

            using (var reader = new StringReader(File.ReadAllText(razorTemplatePath)))
            {
                result = engine.GenerateCode(reader);
            }

            //-- Generate C# code from the CodeDOM graph
            var builder = new StringBuilder();

            using (var writer = new StringWriter(builder, CultureInfo.InvariantCulture))
            {
                new CSharpCodeProvider().GenerateCodeFromCompileUnit(result.GeneratedCode, writer, new CodeGeneratorOptions());
                return(builder.ToString());
            }
        }
Esempio n. 2
0
        public static string GenerateCodeWithRazorEngine(string razorTemplatePath, Type modelType)
        {
            //-- Configure the code-generator
            var host = new RazorEngineHost(new RazorEngine.Compilation.CSharp.CSharpRazorCodeLanguage(true), () => new HtmlMarkupParser())
            {
                DefaultBaseTemplateType = typeof(TemplateBase<>),
                DefaultModelType = modelType,
                DefaultBaseClass = typeof(TemplateBase<>).FullName,
                DefaultClassName = "RazorViewGeneratedByRazorEngine",
                DefaultNamespace = "CompiledRazorTemplates.Dynamic",
                GeneratedClassContext = new GeneratedClassContext("Execute", "Write", "WriteLiteral",
                                                                  "WriteTo", "WriteLiteralTo",
                                                                  "RazorEngine.Templating.TemplateWriter",
                                                                  "DefineSection")
                {
                    ResolveUrlMethodName = "ResolveUrl"
                }
            };

            //-- Parse the template into a CodeDOM graph (see http://msdn.microsoft.com/en-us/library/y2k85ax6(v=vs.110).aspx for an overview of what CodeDOM is)
            var engine = new RazorTemplateEngine(host);
            GeneratorResults result;

            using (var reader = new StringReader(File.ReadAllText(razorTemplatePath)))
            {
                result = engine.GenerateCode(reader);
            }

            //-- Generate C# code from the CodeDOM graph
            var builder = new StringBuilder();
            using (var writer = new StringWriter(builder, CultureInfo.InvariantCulture))
            {
                new CSharpCodeProvider().GenerateCodeFromCompileUnit(result.GeneratedCode, writer, new CodeGeneratorOptions());
                return builder.ToString();
            }
        }