从.net MVC视图得到HTML,而不实际呈现在浏览器中查看? [英] get HTML from .Net MVC View without actually rendering the view in the browser?
问题描述
我在我的控制器,一个ActionResult,我想从,一个由视图生成的电子邮件正文发送HTML电子邮件。而不是在我的控制器2 actionresults方法时,通过我的模型我刚刚得到的观点的结果,并避免被发送到浏览器?
I have an ActionResult in my controller that I want to send an HTML email from, the body of that email is generated by a view. Rather than having 2 actionresults methods in my controller can I just get the result of the view when passed my model and avoid it being sent to the browser?
推荐答案
MvcMailer 一> 是用于支持使用MVC的观点产生电子邮件辉煌的小项目。它可以作为一个的NuGet包。
MvcMailer is a brilliant little project that supports generating emails using MVC views. It is available as a NuGet package.
为了渲染一个视图字符串而不是响应使用code( relativePath
指向您的视图文件):
In order to render a view to a string instead of response use this code (relativePath
points to your view file):
var content = string.Empty;
var view = ViewEngines.Engines.FindView(ControllerContext, relativePath, null);
using (var writer = new StringWriter())
{
var context = new ViewContext(ControllerContext, view.View, ViewData, TempData, writer);
view.View.Render(context, writer);
writer.Flush();
content = writer.ToString();
}
这篇关于从.net MVC视图得到HTML,而不实际呈现在浏览器中查看?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!