c# - RazorEngineService.RunCompile():当前上下文中不存在名称“section”
问题描述
我有这个子布局试图定义一个部分,@section
以在名为_MainLayout.cshtml的主布局中呈现
_SubLayout.cshtml
@{ Layout = "_MainLayout"; }
@section Styles {
<style>
.container { width: 50%; }
</style>
}
<div class="container"></div>
那么这里是预计通过@RenderSection("Styles", false)
_MainLayout.cshtml
<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html charset=UTF-8" />
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Poppins:300,400,500,600,700">
<style>
html,
body {
margin: 0 !important;
padding: 0 !important;
height: 100% !important;
width: 100% !important;
}
@RenderSection("Styles", false)
</style>
</head>
<html>
<body>
@RenderBody()
</body>
</html>
但我在编译剃须刀模板时得到了这个RazorEngineService.RunCompile()
private string RenderTemplate<T>(string view, List<string> layouts, T model)
{
string template = GetTemplate(view);
var config = new TemplateServiceConfiguration();
using (var service = RazorEngineService.Create(config))
{
if (layouts != null)
foreach (var layout in layouts)
{
var templateLayout = GetTemplate(layout, "Shared");
if (!string.IsNullOrEmpty(templateLayout))
service.AddTemplate(layout, templateLayout);
}
return service.RunCompile(template, new Guid().ToString(), typeof(T), model);
};
}
private string GetTemplate(string view, string subFolderName = "")
{
var root = Directory.GetCurrentDirectory();
var location = Path.Combine(root, "Views", "Email");
if(!string.IsNullOrEmpty(subFolderName))
location = Path.Combine(location, subFolderName);
var file = Path.Combine(location, view + ".cshtml");
if (!File.Exists(file))
return "";
return File.ReadAllText(file);
}
解决方案
推荐阅读
- amazon-web-services - AWS IAM:条件上下文键“aws:RequestTag”如何工作?
- javascript - 如何在 Element ui 表中对本地日期进行排序并重新格式化?
- java - 为什么 gmail 会显示使用 MimeMessageHelper addInline 与 Amazon SES 一起发送的图像的“无名”附件?
- mysql - 我可以在 MySQL 中使用一个查询在表中插入(“挤入”)一行并在插入行之后增加剩余值吗?
- javascript - 图片源标签中包含的 SVG img 的链接部分
- kotlin - Kafka Streams 因 Materialized 失败。`as`(STORE_NAME)
- regex - 我写了 url 验证正则表达式,但正则表达式很慢
- c++ - bash 文件并多次运行 c++ 代码
- firebase - 如何将 react-redux-firebase 2.x 迁移到 react-redux-firebase 3.x
- powershell - DSC 节点未重新启动