[英]RazorEngine throws 'the name model does not exist in the current context' at runtime
我正在使用RazorEngine
package 生成電子郵件模板。
這是該方法的代碼:
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 不起作用有關,這根本不是我的情況。
UPD:調用代碼:
*Dapper query*
var fullModel = new WeeklySummaryModel
{
User = user,
BoardThreadWithRepliesModels = models
};
return await _emailTemplateHelper.GetEmailTemplateAsString("WeeklySummary", fullModel);
UPD2:異常表明第 14 行第 18 行存在問題。結果它位於自動生成的代碼中:
// <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 -----------
RazorEngine 是基於 Microsoft 的 Razor 視圖引擎構建的自定義庫。 它不具備 MVC 和 Razor 頁面中您可能熟悉的所有功能。
只需刪除@model
指令。 它應該可以工作,但如果沒有,Visual Studio 在 IntelliSense 的意義上也沒有多大幫助。
這個錯誤:
當前上下文中不存在名稱“模型”
當您嘗試在模板中使用小寫m
引用model的實例時,通常會發生這種情況。
正如這里所解釋的,model 的類型是使用小寫的@model
定義的,但要引用 model 的實例,您必須使用大寫的@Model
或Model
。
我在您的腳本中看不到對視圖 model 的錯誤引用,但這並不意味着它不存在。
嘗試降級 nuget package,版本 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):
...
它應該包含錯誤引用的行號和字符號以及正在編譯模板的臨時文件位置。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.