RazorEngine布局

我使用剃刀引擎https://github.com/Antaris/RazorEngineparsing我的电子邮件模板的正文。 是否可以定义一个布局,并包括其他.cshtml文件? 例如共同的页眉和页脚。

我得到了常见的模板和布局工作,在这两个post的帮助下:

RazorEnginestring布局和部分?

http://blogs.msdn.com/b/hongyes/archive/2012/03/12/using-razor-template-engine-in-web-api-self-host-application.aspx

这是我的解决scheme:

解决scheme1: 布局

通过设置_Layout使用

@{ _Layout = "Layout.cshtml"; ViewBag.Title = Model.Title; } 

页脚

 @section Footer { @RenderPart("Footer.cshtml") } 

Layout.cshtml

 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> </head> <body> <div id="content"> @RenderBody() </div> @if (IsSectionDefined("Footer")) { <div id="footer"> @RenderSection("Footer") </div> } </body> </html> 

TemplateBaseExtensions

用RenderPart方法扩展TemplateBase

 public abstract class TemplateBaseExtensions<T> : TemplateBase<T> { public string RenderPart(string templateName, object model = null) { string path = Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase, "Templates", templateName); return Razor.Parse(File.ReadAllText(path), model); } } 

剃刀configuration

将BaseTemplateType设置为您的TemplateBaseExtensions类

 TemplateServiceConfiguration templateConfig = new TemplateServiceConfiguration { BaseTemplateType = typeof(TemplateBaseExtensions<>) }; Razor.SetTemplateService(new TemplateService(templateConfig)); 

编辑解决scheme2:

如果您正在使用TemplateResolver。 RenderPart不需要使用@Include来代替

页脚

 @section Footer { @Include("Footer.cshtml") } 

分解器

 public class TemplateResolver : ITemplateResolver { public string Resolve(string name) { if (name == null) { throw new ArgumentNullException("name"); } string path = Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase, "Templates", name); return File.ReadAllText(path, System.Text.Encoding.Default); } } 

configuration

 TemplateServiceConfiguration templateConfig = new TemplateServiceConfiguration { Resolver = new TemplateResolver() }; Razor.SetTemplateService(new TemplateService(templateConfig)); 

由松饼人更新指定一个模板并呈现一个string

 var templateResolver = Razor.Resolve("Registration.cshtml"); return templateResolver.Run(new ExecuteContext()); 

此外,我和其他人在这个链接https://github.com/Antaris/RazorEngine/issues/61有使用;_Layout问题,而Layout工作。

“_Layout”是旧的语法。 它在未来的版本中更新为“布局”。

你可以用剃刀轻松做很多事情。 然而,这个特定的项目似乎将许多你可以做的Razor引擎抽象掉了(这是好的和坏的)。 在你的情况下,这听起来像你会更好地执行自己的Razor解决scheme(它实际上并不坏),然后你可以让你的模板抛出exception或很容易地拉其他内容。

例如; 通过滚动你自己的解决scheme,你可以为你的razor模板创build一个基类,通过调用其他模板来展示容纳的“部分视图”。 另外,如果某些属性为空,则可以执行模型检查并引发exception。

使用RazorEngine实现布局的最简单的方法是通过在布局的@RenderBody()中replace模板的返回值:

  var finalHtml = layout.Replace(@"@RenderBody()", templateHtml); 

例如:

你的_Layout.cshtml与典型的@RenderBody()

 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> </head> <body> <div> @RenderBody() </div> </body> </html> 

您的RazorEngine模板MyTemplate.cshtml

 @using RazorEngine.Templating @inherits TemplateBase<myviewmodel> <h1>Hello People</h1> <p>@Model</p> 

无论你在哪里调用RazorEngine模板:

 var TemplateFolderPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "EmailTemplates"); var template = File.ReadAllText(Path.Combine(TemplateFolderPath,"MyTemplate.cshtml")); var layout = File.ReadAllText(Path.Combine(TemplateFolderPath, "_Layout.cshtml")); var templateService = new TemplateService(); var templateHtml = templateService.Parse(template, myModel, null, null); var finalHtml = layout.Replace(@"@RenderBody()", templateHtml);