在 Web 表单应用程序中使用 Razor 视图作为电子邮件模板
我有一个需要发送电子邮件的 asp.net Web 表单应用程序。我喜欢 Razor 语法,因为您可以在 MVC 之外使用 Razor,所以我想我会尝试使用它。我已经看到您可以以编程方式将模板字符串传递给 razor,但我想将我的 razor 模板保留为单独的 .cshtml 文件。
有人对如何做到这一点有任何简单、防白痴的建议吗?我尝试从文件中加载它们,如下所示:
UserDetails userDetails = new UserDetails {Name = "Fred"};
string template = File.OpenText("Email/UserDetailsEmail.cshtml").ReadToEnd();
string messageText = Razor.Parse(template, userDetails);
一切都无济于事。查找文件时出现异常。我正在使用 razorengine dll。
我的其他一切都可以工作,smtp 服务器等,只是视图不行。
任何帮助表示赞赏。
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);
All to no avail. There's an exception finding the file. I'm using the razorengine dll.
I have everything else working, the smtp server etc, just not the views.
Any help appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要获取文件的完整路径;相对路径最终将相对于错误的位置。
写
You need to get the full path to the file; relative paths will end up being relative to the wrong location.
Write
我会使用 MVCMailer 工具,您可以使用 Nuget 设置该工具,并具有用于创建视图的脚手架。
这是一个编写得非常好的应用程序,可以轻松添加到您的应用程序中:
https://github.com/smsohan/MvcMailer< /a>
http://www.codeproject.com/KB/aspnet/MvcMailerNuGet.aspx
I would use the MVCMailer tool which you can setup with Nuget and has scaffolding for creating the views.
It is a very well written app and can be added to yours easily:
https://github.com/smsohan/MvcMailer
http://www.codeproject.com/KB/aspnet/MvcMailerNuGet.aspx