public async Task<TemplateResult> RunTemplateAsync(string content,
            dynamic templateModel)
        {
            RazorTemplatingHost host = new RazorTemplatingHost(typeof(RazorTemplateBase));
            RazorTemplateEngine engine = new RazorTemplateEngine(host);

            using (var reader = new StringReader(content))
            {
                var generatorResults = engine.GenerateCode(reader);

                if (!generatorResults.Success)
                {
                    var messages = generatorResults.ParserErrors.Select(e => e.Message);
                    return new TemplateResult()
                    {
                        GeneratedText = string.Empty,
                        ProcessingException = new TemplateProcessingException(messages, generatorResults.GeneratedCode)
                    };
                }

                var templateResult = _compilationService.Compile(generatorResults.GeneratedCode);
                if (templateResult.Messages.Any())
                {
                    return new TemplateResult()
                    {
                        GeneratedText = string.Empty,
                        ProcessingException = new TemplateProcessingException(templateResult.Messages, generatorResults.GeneratedCode)
                    };
                }

                var compiledObject = Activator.CreateInstance(templateResult.CompiledType);
                var razorTemplate = compiledObject as RazorTemplateBase;

                string result = String.Empty;
                if (razorTemplate != null)
                {
                    razorTemplate.Model = templateModel;
                    //ToDo: If there are errors executing the code, they are missed here.
                    result = await razorTemplate.ExecuteTemplate();
                }

                return new TemplateResult()
                {
                    GeneratedText = result,
                    ProcessingException = null
                };
            }
        }
Beispiel #2
0
        public async Task <TemplateResult> RunTemplateAsync(string content,
                                                            dynamic templateModel)
        {
            RazorTemplatingHost host   = new RazorTemplatingHost(typeof(RazorTemplateBase));
            RazorTemplateEngine engine = new RazorTemplateEngine(host);

            using (var reader = new StringReader(content))
            {
                var generatorResults = engine.GenerateCode(reader);

                if (!generatorResults.Success)
                {
                    var messages = generatorResults.ParserErrors.Select(e => e.Message);
                    return(new TemplateResult()
                    {
                        GeneratedText = string.Empty,
                        ProcessingException = new TemplateProcessingException(messages, generatorResults.GeneratedCode)
                    });
                }

                var templateResult = _compilationService.Compile(generatorResults.GeneratedCode);
                if (templateResult.Messages.Any())
                {
                    return(new TemplateResult()
                    {
                        GeneratedText = string.Empty,
                        ProcessingException = new TemplateProcessingException(templateResult.Messages, generatorResults.GeneratedCode)
                    });
                }

                var compiledObject = Activator.CreateInstance(templateResult.CompiledType);
                var razorTemplate  = compiledObject as RazorTemplateBase;

                string result = String.Empty;
                if (razorTemplate != null)
                {
                    razorTemplate.Model = templateModel;
                    //ToDo: If there are errors executing the code, they are missed here.
                    result = await razorTemplate.ExecuteTemplate();
                }

                return(new TemplateResult()
                {
                    GeneratedText = result,
                    ProcessingException = null
                });
            }
        }