用剃刀意见电子邮件模板Web窗体应用程序内 [英] Use Razor views as email templates inside a Web Forms app

查看:129
本文介绍了用剃刀意见电子邮件模板Web窗体应用程序内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个asp.net Web窗体应用程序需要发送电子邮件。我喜欢的剃刀语法和因为你可以使用剃刀MVC之外我想我会尝试并使用它。我已经看到了你可以通过编程方式传递一个模板字符串剃刀,但我想保持我的剃须刀模板作为单独的.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.

任何帮助AP preciated。

Any help appreciated.

推荐答案

您需要得到完整的文件路径;相对路径最终会被相对错误的位置。

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窗体应用程序内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆