我想利用Razor View的模型绑定/渲染功能为我从ASP.NET MVC应用程序发送的电子邮件生成HTML正文内容。
有没有办法将视图呈现给字符串而不是将其作为GET请求的ActionResult返回?
为了说明我正在寻找能够做到以下事情的事情......
public ActionResult SendEmail(int id)
{
EmailDetailsViewModel emailDetails = EmailDetailsViewModel().CreateEmailDetails(id);
// THIS IS WHERE I NEED HELP...
// I want to pass my ViewModel (emailDetails) to my View (EmailBodyRazorView) but instead of Rending that to the Response stream I want to capture the output and pass it to an email client.
string htmlEmailBody = View("EmailBodyRazorView", emailDetails).ToString();
// Once I have the htmlEmail body I'm good to go. I've got a utilityt that will send the email for me.
MyEmailUtility.SmtpSendEmail("stevejobs@apple.com", "Email Subject", htmlEmailBody);
// Redirect another Action that will return a page to the user confirming the email was sent.
return RedirectToAction("ConfirmationEmailWasSent");
}
答案 0 :(得分:28)
如果您只需要将视图渲染为字符串,请尝试以下方法:
public string ToHtml(string viewToRender, ViewDataDictionary viewData, ControllerContext controllerContext)
{
var result = ViewEngines.Engines.FindView(controllerContext, viewToRender, null);
StringWriter output;
using (output = new StringWriter())
{
var viewContext = new ViewContext(controllerContext, result.View, viewData, controllerContext.Controller.TempData, output);
result.View.Render(viewContext, output);
result.ViewEngine.ReleaseView(controllerContext, result.View);
}
return output.ToString();
}
您需要从控制器操作传递视图的名称以及ViewData和ControllerContext。
答案 1 :(得分:10)
您可以结帐Postal以使用观看发送电子邮件。
答案 2 :(得分:4)
答案 3 :(得分:3)
另一个是ActionMailer.Net:https://bitbucket.org/swaj/actionmailer.net/wiki/Home
从网站:一个基于MVC 3的Rails ActionMailer库到ASP.NET MVC的端口。我们的目标是让您的应用程序发送电子邮件变得轻松而且相对轻松。
NuGet:Install-Package ActionMailer
答案 4 :(得分:1)
NuGet还有Essential Mail: Razor个包。它构建在RazorEngine之上,为电子邮件呈现提供了简单的界面。
电子邮件信息模板类似于
@inherits Essential.Templating.Razor.Email.EmailTemplate
@using System.Net;
@{
From = new MailAddress("example@email.com");
Subject = "Email Subject";
}
@section Html
{
<html>
<head>
<title>Example</title>
</head>
<body>
<h1>HTML part of the email</h1>
</body>
</html>
}
@section Text
{
Text part of the email.
}
该项目托管在GitHub上:https://github.com/smolyakoff/essential-templating/wiki/Email-Template-with-Razor
答案 5 :(得分:0)
根据Ryan的回答,我做了一个扩展方法:
public static string RenderViewToString(this Controller source, string viewName)
{
var viewEngineResult = ViewEngines.Engines.FindView(source.ControllerContext, viewName, null);
using (StringWriter output = new StringWriter())
{
viewEngineResult.View.Render(new ViewContext(source.ControllerContext, viewEngineResult.View, source.ViewData, source.TempData, output), output);
viewEngineResult.ViewEngine.ReleaseView(source.ControllerContext, viewEngineResult.View);
return output.ToString();
}
}
从控制器内部调用(示例用法):
[AllowAnonymous]
public class ErrorController : Controller
{
// GET: Error
public ActionResult Index(System.Net.HttpStatusCode id)
{
Exception ex = null; // how do i get the exception that was thrown?
if (!Debugger.IsAttached)
Code.Email.Send(ConfigurationManager.AppSettings["BugReportEmailAddress"],
$"Bug Report: AgentPortal: {ex?.Message}",
this.RenderViewToString("BugReport"));
Response.StatusCode = (int)id;
return View();
}
}
答案 6 :(得分:0)
仍然使用最新的MVC FullFW在Web应用程序外部工作
http://fabiomaulo.blogspot.com/2011/08/parse-string-as-razor-template.html
您可以创建一个使用队列渲染并在网络外部发送电子邮件的工作人员。 很少的代码行,您不需要Razor上的其他软件包。