- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我一直在尝试在我的一个小项目中使用 RazorEngine,但是当我尝试使用模板布局时无法克服这个错误。
Unable to compile template. 'object' does not contain a definition for 'Description' and no extension method 'Description' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)
我的设置:我有一个这样的模板布局:
<html>
<head>
<title>@Model.Description</title>
</head>
<body>
@RenderBody()
</body>
</html>
然后是一个如下所示的页面模板:
@{
_Layout = "Layout.cshtml";
}
<h1>@Model.Description</h1>
这是我用来尝试解决这个问题的测试 Main 函数:
static void Main(string[] args)
{
// Configuration for RazorEngine
var config = new TemplateServiceConfiguration
{
EncodedStringFactory = new RawStringFactory(),
Resolver = new DelegateTemplateResolver(name =>
{
var file = name;
var content = File.ReadAllText("Templates/" + file);
return content;
})
};
// Try to render output using Razor
using (var service = new TemplateService(config))
{
string template = File.ReadAllText("Templates/Default.cshtml");
dynamic model = new ExpandoObject();
model.Description = "This is a test";
string result = service.Parse(template, model);
Console.WriteLine(result);
if (Debugger.IsAttached)
{
Console.ReadLine();
}
}
}
知道我错过了什么吗?
更新:如果我将动态模型对象替换为具有 Description 属性的 POCO,它就会起作用。我还用
尝试了 Parse 的类型化版本dynamic
, ExpandoObject
, 和 IDictionary<string, object>
但他们都有同样的错误。
更新:我在 Github 上发现了这个项目,它似乎以某种方式工作: https://github.com/mikoskinen/graze/blob/master/src/core/Graze.cs#L174
最佳答案
我认为使用模板服务的 Parse
重载更合适,它采用 ViewBag
并在您的 View 中使用 ViewBag
而不是模型。
编辑:
public virtual string Parse(string razorTemplate, object model, DynamicViewBag viewBag, string cacheName)
关于c# - RazorEngine 和布局的 TemplateCompilationError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13328114/
我的 RazorEngine 版本 3.6.3 有问题RazorEngine 无法从“字符串”转换为“RazorEngine.Templated.ITemplateKey” 我的代码如下:
我有一个控制台应用程序,它使用 RazorEngine 根据应用程序的运行发送电子邮件。我成功地能够在开发 IDE 中和通过可执行文件运行控制台应用程序发送电子邮件。但是,当我从计划任务运行控制台应用
当您调用 RazorEngine.Razor.Compile() ,编译后的模板存放在哪里? 程序重新启动后是否可用?如果内存不足,会被转储吗? 我正在使用 RazorEngine在 ASP.NET
我正在使用RazorEngine渲染一些基本内容(一个非常粗糙的内容管理系统)。 除非我在标记中包含任何@Html语法,否则它的效果很好。 如果标记包含@html,则会出现以下错误: Unable t
在我的页面中,我想显示这样的字符串: Today is (x)th of july 其中 (x) 由程序在运行时计算并作为@Model.Number 传递给 RazorEngine 但是下面的模板格式
目前,我正在使用 RazorEngine v2.1 作为发送模板化电子邮件(数千封)的后台进程的一部分。为了加快速度,模板以其 md5 和作为名称进行编译。这样一来,当模板更改时,它会被重新编译,并且
我正在尝试用这一行编译一个 razor 文件 RazorEngine.Razor.Parse("Hello world"); 但它只是抛出 base {System.Exception} = {"Un
我像这样使用 Razor 引擎: public class EmailService : IService { private readonly ITemplateService templa
我正在使用 Razor 引擎 https://github.com/Antaris/RazorEngine解析我的电子邮件模板的正文。是否可以定义布局并包含其他 .cshtml 文件?例如常见的页眉和
string templ = @"@Model.Name[0] @Model.Name[1]"; string result = RazorEngine.Razor.Parse(templ, new
我们有一个使用 RazorEngine 解析电子邮件内容并使用 Web API 客户端发送电子邮件的类库。 我们使用它通过控制台应用程序定期发送时事通讯。这非常有效,因为它每天可以发送 50k+ 封电
我正在使用 RazorEngine 构建一个电子邮件模板系统,并希望在与我的主模板不同的 cshtml 文件中维护辅助函数。例如,假设我有一个这样的文件夹结构: /Templates Supp
这是对 THIS 的更新问题是 V3 已经有一段时间了: 我也在考虑将 RazorEngine 用于邮件模板(甚至可能更多)。 现在是否有“足够的安全模式”可供用户定义模板? 使用 IsolatedT
当 razor 模板有一个包含在括号中的变量时(例如下面代码中的“(@Model.B)”),如果该变量前面有“ 标志来查找将用作 html 的标签。 要解决您的问题,您可以使用 < 的 xml 替换
我正在使用 Razor 引擎:https://github.com/Antaris/RazorEngine . 我正在动态创建模型。我试图在模板中包含一个扩展方法,但它会识别扩展类。我收到以下错误消息
我正在尝试使用 RazorEngine 将其他文件包含在模板中,但我有点卡住了。我掌握了基础知识,但我希望能够在我的模板中使用 @Include("somefile.html")。 这是我到现在为止得
我一直在尝试在我的一个小项目中使用 RazorEngine,但是当我尝试使用模板布局时无法克服这个错误。 Unable to compile template. 'object' does not c
是否可以知道特定模板是否已使用 RazorEngine 编译?基本上,如果您调用: Razor.Parse("Hello there @Model.Name", model, "hello-world
我注意到 RazorEngine.Compile() 似乎以不同于其他类型的方式对待匿名类型。例如,考虑以下代码: public static void Main() { try {
我有类似邮件合并的功能,它采用模板、一些业务对象,并生成 html,然后将其制作成 PDF。 我正在使用 RazorEngine将模板+模型做成 html 位。 如果我让用户指定模板,我要冒什么风险?
我是一名优秀的程序员,十分优秀!