我想利用 Razor 视图的模型绑定/渲染功能为我从 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("[email protected]", "Email Subject", htmlEmailBody);
// Redirect another Action that will return a page to the user confirming the email was sent.
return RedirectToAction("ConfirmationEmailWasSent");
}
如果您只需要将视图渲染为字符串,请尝试以下操作:
public string ToHtml(string viewToRender, ViewDataDictionary viewData, ControllerContext controllerContext)
{
var result = ViewEngines.Engines.FindView(controllerContext, viewToRender, null);
using (var 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。
您可以查看Postal以使用视图发送电子邮件。
尝试 MvcMailer: http://www.codeproject.com/KB/aspnet/MvcMailerNuGet.aspx
另一个是 ActionMailer.Net:https://bitbucket.org/swaj/actionmailer.net/wiki/Home
来自网站:Rails ActionMailer 库到 ASP.NET MVC 的基于 MVC 3 的端口。目标是让从应用程序发送电子邮件变得简单且相对轻松。
NuGet:
Install-Package ActionMailer
还有来自 NuGet 的 Essential Mail: Razor 包。它基于 RazorEngine 构建,并为电子邮件渲染提供简单的界面。
电子邮件消息模板看起来像这样
@inherits Essential.Templating.Razor.Email.EmailTemplate
@using System.Net;
@{
From = new MailAddress("[email protected]");
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-templated/wiki/Email-Template-with-Razor
根据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();
}
}
仍在使用最后一个 MVC FullFW 在 Web 应用程序之外工作
http://fabiomaulo.blogspot.com/2011/08/parse-string-as-razor-template.html
您可以创建一个使用队列渲染并在网络外部发送电子邮件的工作人员。 代码行很少,您不需要 Razor 上的另一个包。