Use Razor views as email templates inside a Web Forms app(在 Web 窗体应用程序中使用 Razor 视图作为电子邮件模板)
问题描述
我有一个需要发送电子邮件的 asp.net Web 窗体应用程序.我喜欢 Razor 语法,因为您可以在 MVC 之外使用 Razor,所以我想我会尝试使用它.我已经看到您可以通过编程方式将模板字符串传递给 razor,但我想将我的 razor 模板保留为单独的 .cshtml 文件.
I have an asp.net Web Forms application that needs to send emails. I like the Razor syntax and as you can use Razor outside of MVC I thought I'd try and use that. I've seen that you can programmatically pass a template string to razor but I'd like to keep my razor templates as separate .cshtml files.
有没有人有任何关于如何做到这一点的简单、防白痴的建议?我试过从这样的文件中加载它们:
Does anyone have any simple, idiot-proof advice as to how to do this? I've tried to load them from file like this:
UserDetails userDetails = new UserDetails {Name = "Fred"};
string template = File.OpenText("Email/UserDetailsEmail.cshtml").ReadToEnd();
string messageText = Razor.Parse(template, userDetails);
一切都无济于事.查找文件时出现异常.我正在使用 razorengine dll.
All to no avail. There's an exception finding the file. I'm using the razorengine dll.
我有其他一切工作,smtp 服务器等,只是没有视图.
I have everything else working, the smtp server etc, just not the views.
感谢任何帮助.
推荐答案
您需要获取文件的完整路径;相对路径最终会相对于错误的位置.
You need to get the full path to the file; relative paths will end up being relative to the wrong location.
写
File.OpenText(Server.MapPath("~/Email/UserDetailsEmail.cshtml"))
这篇关于在 Web 窗体应用程序中使用 Razor 视图作为电子邮件模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Web 窗体应用程序中使用 Razor 视图作为电子邮


基础教程推荐
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01