【发布时间】:2015-11-19 08:17:40
【问题描述】:
在我的 C# 项目中,我在 App_Code 文件夹下有一个名为 MyHelpers.cshtml 的文件,其中包含以下代码:
@helper MakeNote(string content) {
<div class="note"
style="border: 1px solid black; width: 90%; padding: 5px; margin-left: 15px;">
<p>
<strong>Note</strong> @content
</p>
</div>
}
我直接在根文件夹下创建了一个MyTest.cshtml,内容如下:
<!DOCTYPE html>
<head>
<title>Test Helpers Page</title>
</head>
<body>
<p>This is some opening paragraph text.</p>
<!-- Insert the call to your note helper here. -->
@MyHelpers.MakeNote("My test note content.")
<p>This is some following text.</p>
</body>
</html>
App_Code/MyHelpers.cshtml 在从 MyTest.cshtml 调用时有效。
我正在使用 RazorEngine 生成 HTML 电子邮件,我的实际模板文件存储在名为 EmailTemplates 的文件夹中。当我在 EmailTemplates 的任何 CSHTML 文件中插入与 MyTest.cshtml 相同的行 @MyHelpers.MakeNote("My test note content.") 时,我收到以下错误:
The server encountered an error processing the request.
The exception message is 'Unable to compile template.
The name 'MyHelpers' does not exist in the current context.
Other compilation errors may have occurred.
Check the Errors property for more information.'
如何获取 EmailTemplates 文件夹中的 CSHTML 文件以使用 App_Code/MyHelpers.cshtml 中的 MakeNote 功能,就像 MyTest.cshtml 使用它一样?
【问题讨论】:
-
EmailTemplates是Views文件夹的子目录吗? -
它确实对我有用。我可以从我项目的任何目录中的任何
.cshtml页面访问MyHelpers。 -
@YeldarKurmangaliyev 问题不是从任何页面访问助手,而是从页面访问任何助手(即助手的位置,而不是视图的位置)
-
有点旧,但可能仍然适用:stackoverflow.com/questions/14582243/…
-
@freedomn-m 是的,我明白了。我的意思是
@MyHelpers.MakeNote("some string")在我项目的每个.cshtml中都有效。
标签: c# asp.net-mvc razor