RazorEngine 在运行时抛出 'the name model does not exist in the current context'
RazorEngine throws 'the name model does not exist in the current context' at runtime
我正在使用 RazorEngine
包生成 e-mail 模板。
这是该方法的代码:
public async Task<string> GetEmailTemplateAsString<T>(string viewName, T model)
{
var templatePath = @$"{Directory.GetCurrentDirectory()}\Views\{viewName}.cshtml";
var template = await File.ReadAllTextAsync(templatePath);
var html = Engine.Razor.RunCompile(template, "weeklySummary", typeof(T), model);
return html;
}
和视图:
@model CourseWork.Core.Models.EmailTemplate.WeeklySummaryModel
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
body {
background-color: lightgoldenrodyellow;
font-family: Verdana, Geneva, Tahoma, sans-serif;
}
.greetings {
text-align: center;
border-bottom: 1px solid lightgray;
}
.main {
display: block;
border-bottom: 1px solid lightgray;
text-align: center;
padding-bottom: 21.28px;
}
ul {
list-style: none;
padding: 0;
margin: 0;
}
li {
display: block;
padding: 10px;
}
</style>
</head>
<body>
<section class="greetings">
<h1>Hello, @Model.User.DisplayName!</h1>
<h3>Here is our weekly summary, specially for you</h3>
</section>
<section class="main">
@foreach (var entry in Model.BoardThreadWithRepliesModels)
{
<h4>@entry.BoardName</h4>
<h5>@entry.ThreadName</h5>
<ul>
@foreach (var reply in entry.Replies)
{
<li>
<span>@reply.UserDisplayName</span>
<p>@reply.Content</p>
<img src="@reply.PicRelatedPath" alt="pic-related" />
</li>
}
</ul>
}
</section>
</body>
</html>
在我 运行 应用程序之后,我收到 CS0103 错误,问题的标题中有描述。
我尝试用谷歌搜索错误消息,但主要是所有结果都与 IntelliSense 不工作有关,这根本不是我的情况。
更新:
调用代码:
*Dapper query*
var fullModel = new WeeklySummaryModel
{
User = user,
BoardThreadWithRepliesModels = models
};
return await _emailTemplateHelper.GetEmailTemplateAsString("WeeklySummary", fullModel);
UPD2:
异常表明第 14 行第 18 行存在问题。它原来位于 auto-generated 代码中:
// <auto-generated/>
#pragma warning disable 1591
namespace CompiledRazorTemplates.Dynamic
{
#line hidden
using System.Threading.Tasks;
using System;
using System.Collections.Generic;
using System.Linq;
internal class RazorEngine_00fefb8ea0984fabaf601e182158fa32 : RazorEngine.Templating.TemplateBase<dynamic>
{
#pragma warning disable 1998
public async override global::System.Threading.Tasks.Task ExecuteAsync()
{
Write(model);
WriteLiteral(@" CourseWork.Core.Models.EmailTemplate.WeeklySummaryModel
<html lang=""en"">
<head>
<meta charset=""UTF-8"">
<meta http-equiv=""X-UA-Compatible"" content=""IE=edge"">
<meta name=""viewport"" content=""width=device-width, initial-scale=1.0"">
<title>Document</title>
<style>
body {
background-color: lightgoldenrodyellow;
font-family: Verdana, Geneva, Tahoma, sans-serif;
}
.greetings {
text-align: center;
border-bottom: 1px solid lightgray;
}
.main {
display: block;
border-bottom: 1px solid lightgray;
text-align: center;
padding-bottom: 21.28px;
}
ul {
list-style: none;
padding: 0;
margin: 0;
}
li {
display: block;
padding: 10px;
}
</style>
</head>
<body>
<section class=""greetings"">
<h1>Hello, ");
Write(Model.User.DisplayName);
WriteLiteral("!</h1>\r\n <h3>Here is our weekly summary, specially for you</h3>\r\n </section>\r\n\r\n <section class=\"main\">\r\n");
foreach (var entry in Model.BoardThreadWithRepliesModels)
{
WriteLiteral(" <h4>");
Write(entry.BoardName);
WriteLiteral("</h4>\r\n <h5>");
Write(entry.ThreadName);
WriteLiteral("</h5>\r\n <ul>\r\n");
foreach (var reply in entry.Replies)
{
WriteLiteral(" <li>\r\n <span>");
Write(reply.UserDisplayName);
WriteLiteral("</span>\r\n <p>");
Write(reply.Content);
WriteLiteral("</p>\r\n <img");
BeginWriteAttribute("src", " src=\"", 1563, "\"", 1590, 1);
WriteAttributeValue("", 1569, reply.PicRelatedPath, 1569, 21, false);
EndWriteAttribute();
WriteLiteral(" alt=\"pic-related\" />\r\n </li>\r\n");
}
WriteLiteral(" </ul>\r\n");
}
WriteLiteral(" </section>\r\n</body>\r\n</html>");
}
#pragma warning restore 1998
}
}
#pragma warning restore 1591
------------- END -----------
这个错误:
The name 'model' does not exist in the current context
通常在您尝试使用小写 m
.
引用模板中的 模型 实例时发生
As explained here,模型的 type 是使用小写 @model
定义的,但是要引用模型的实例,您必须使用大写@Model
或 Model
.
I cannot see nay incorrect reference to the view model in your script, but that doesn't mean it is not there.
尝试降级 nuget 包,版本 3 运行时完整异常应该包含更多信息,如下所示:
Errors while compiling a Template.
Please try the following to solve the situation:
* If the problem is about missing/invalid references or multiple defines either try to load
the missing references manually (in the compiling appdomain!) or
Specify your references manually by providing your own IReferenceResolver implementation.
See https://antaris.github.io/RazorEngine/ReferenceResolver.html for details.
Currently all references have to be available as files!
* If you get 'class' does not contain a definition for 'member':
try another modelType (for example 'null' to make the model dynamic).
NOTE: You CANNOT use typeof(dynamic) to make the model dynamic!
Or try to use static instead of anonymous/dynamic types.
More details about the error:
- error: (126, 58) The name 'model' does not exist in the current context
Temporary files of the compilation can be found in (please delete the folder):
...
它应该包含错误引用的行号和字符号以及编译模板的临时文件位置。
RazorEngine 是构建在 Microsoft 的 Razor 视图引擎之上的自定义库。它不具备您可能熟悉的 MVC 和 Razor 页面的所有功能。
只需删除 @model
指令。它应该可以工作,但如果没有,Visual Studio 无论如何在 IntelliSense 的意义上都没有多大帮助。
我正在使用 RazorEngine
包生成 e-mail 模板。
这是该方法的代码:
public async Task<string> GetEmailTemplateAsString<T>(string viewName, T model)
{
var templatePath = @$"{Directory.GetCurrentDirectory()}\Views\{viewName}.cshtml";
var template = await File.ReadAllTextAsync(templatePath);
var html = Engine.Razor.RunCompile(template, "weeklySummary", typeof(T), model);
return html;
}
和视图:
@model CourseWork.Core.Models.EmailTemplate.WeeklySummaryModel
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
body {
background-color: lightgoldenrodyellow;
font-family: Verdana, Geneva, Tahoma, sans-serif;
}
.greetings {
text-align: center;
border-bottom: 1px solid lightgray;
}
.main {
display: block;
border-bottom: 1px solid lightgray;
text-align: center;
padding-bottom: 21.28px;
}
ul {
list-style: none;
padding: 0;
margin: 0;
}
li {
display: block;
padding: 10px;
}
</style>
</head>
<body>
<section class="greetings">
<h1>Hello, @Model.User.DisplayName!</h1>
<h3>Here is our weekly summary, specially for you</h3>
</section>
<section class="main">
@foreach (var entry in Model.BoardThreadWithRepliesModels)
{
<h4>@entry.BoardName</h4>
<h5>@entry.ThreadName</h5>
<ul>
@foreach (var reply in entry.Replies)
{
<li>
<span>@reply.UserDisplayName</span>
<p>@reply.Content</p>
<img src="@reply.PicRelatedPath" alt="pic-related" />
</li>
}
</ul>
}
</section>
</body>
</html>
在我 运行 应用程序之后,我收到 CS0103 错误,问题的标题中有描述。
我尝试用谷歌搜索错误消息,但主要是所有结果都与 IntelliSense 不工作有关,这根本不是我的情况。
更新: 调用代码:
*Dapper query*
var fullModel = new WeeklySummaryModel
{
User = user,
BoardThreadWithRepliesModels = models
};
return await _emailTemplateHelper.GetEmailTemplateAsString("WeeklySummary", fullModel);
UPD2: 异常表明第 14 行第 18 行存在问题。它原来位于 auto-generated 代码中:
// <auto-generated/>
#pragma warning disable 1591
namespace CompiledRazorTemplates.Dynamic
{
#line hidden
using System.Threading.Tasks;
using System;
using System.Collections.Generic;
using System.Linq;
internal class RazorEngine_00fefb8ea0984fabaf601e182158fa32 : RazorEngine.Templating.TemplateBase<dynamic>
{
#pragma warning disable 1998
public async override global::System.Threading.Tasks.Task ExecuteAsync()
{
Write(model);
WriteLiteral(@" CourseWork.Core.Models.EmailTemplate.WeeklySummaryModel
<html lang=""en"">
<head>
<meta charset=""UTF-8"">
<meta http-equiv=""X-UA-Compatible"" content=""IE=edge"">
<meta name=""viewport"" content=""width=device-width, initial-scale=1.0"">
<title>Document</title>
<style>
body {
background-color: lightgoldenrodyellow;
font-family: Verdana, Geneva, Tahoma, sans-serif;
}
.greetings {
text-align: center;
border-bottom: 1px solid lightgray;
}
.main {
display: block;
border-bottom: 1px solid lightgray;
text-align: center;
padding-bottom: 21.28px;
}
ul {
list-style: none;
padding: 0;
margin: 0;
}
li {
display: block;
padding: 10px;
}
</style>
</head>
<body>
<section class=""greetings"">
<h1>Hello, ");
Write(Model.User.DisplayName);
WriteLiteral("!</h1>\r\n <h3>Here is our weekly summary, specially for you</h3>\r\n </section>\r\n\r\n <section class=\"main\">\r\n");
foreach (var entry in Model.BoardThreadWithRepliesModels)
{
WriteLiteral(" <h4>");
Write(entry.BoardName);
WriteLiteral("</h4>\r\n <h5>");
Write(entry.ThreadName);
WriteLiteral("</h5>\r\n <ul>\r\n");
foreach (var reply in entry.Replies)
{
WriteLiteral(" <li>\r\n <span>");
Write(reply.UserDisplayName);
WriteLiteral("</span>\r\n <p>");
Write(reply.Content);
WriteLiteral("</p>\r\n <img");
BeginWriteAttribute("src", " src=\"", 1563, "\"", 1590, 1);
WriteAttributeValue("", 1569, reply.PicRelatedPath, 1569, 21, false);
EndWriteAttribute();
WriteLiteral(" alt=\"pic-related\" />\r\n </li>\r\n");
}
WriteLiteral(" </ul>\r\n");
}
WriteLiteral(" </section>\r\n</body>\r\n</html>");
}
#pragma warning restore 1998
}
}
#pragma warning restore 1591
------------- END -----------
这个错误:
The name 'model' does not exist in the current context
通常在您尝试使用小写 m
.
As explained here,模型的 type 是使用小写 @model
定义的,但是要引用模型的实例,您必须使用大写@Model
或 Model
.
I cannot see nay incorrect reference to the view model in your script, but that doesn't mean it is not there.
尝试降级 nuget 包,版本 3 运行时完整异常应该包含更多信息,如下所示:
Errors while compiling a Template.
Please try the following to solve the situation:
* If the problem is about missing/invalid references or multiple defines either try to load
the missing references manually (in the compiling appdomain!) or
Specify your references manually by providing your own IReferenceResolver implementation.
See https://antaris.github.io/RazorEngine/ReferenceResolver.html for details.
Currently all references have to be available as files!
* If you get 'class' does not contain a definition for 'member':
try another modelType (for example 'null' to make the model dynamic).
NOTE: You CANNOT use typeof(dynamic) to make the model dynamic!
Or try to use static instead of anonymous/dynamic types.
More details about the error:
- error: (126, 58) The name 'model' does not exist in the current context
Temporary files of the compilation can be found in (please delete the folder):
...
它应该包含错误引用的行号和字符号以及编译模板的临时文件位置。
RazorEngine 是构建在 Microsoft 的 Razor 视图引擎之上的自定义库。它不具备您可能熟悉的 MVC 和 Razor 页面的所有功能。
只需删除 @model
指令。它应该可以工作,但如果没有,Visual Studio 无论如何在 IntelliSense 的意义上都没有多大帮助。