简体   繁体   English

ASP.NET MVC渲染视图到字符串以进行电子邮件发送

[英]ASP.NET MVC Render View to a string for emailing

I want to use MVC views to create the body for an email and I have come across this ( http://www.brightmix.com/blog/renderpartial-to-string-in-asp-net-mvc/ ) but it doesn't seem to work with strongly typed views (ViewContext is null). 我想使用MVC视图来创建电子邮件的正文,我遇到过这个问题( http://www.brightmix.com/blog/renderpartial-to-string-in-asp-net-mvc/ )但它没有似乎适用于强类型视图(ViewContext为null)。 But I was after something the would render a full view including a masterpage. 但是我想要的东西会呈现包括母版页在内的完整视图。

I thought that if there was a way of just invoking a view with out redirecting and just writing to a different stream and sending the email within the controller that might do it, but I can't work out how to invoke a view. 我想如果有一种方法只是调用一个没有重定向的视图,只是写一个不同的流并在控制器内发送可能会这样做的电子邮件,但我无法弄清楚如何调用一个视图。

Any suggestions would be great! 任何建议都会很棒!

Thanks in advance. 提前致谢。

Gifster Gifster

The question has been asked (and answered) already: 已经提出(并已回答)这个问题了:

Render View as a String 将视图渲染为字符串

This is the bit that I use: 这是我使用的一点:

protected string RenderViewToString<T>(string viewPath, T model, System.Web.Mvc.ControllerContext controllerContext) {
        using (var writer = new StringWriter()) {
            var view = new WebFormView(viewPath);
            var vdd = new ViewDataDictionary<T>(model);
            var viewCxt = new ViewContext(controllerContext, view, vdd, new TempDataDictionary(), writer);
            viewCxt.View.Render(viewCxt, writer);
            return writer.ToString();
        }
    }

Best place for this method is in a class library project that your mvc project has a reference to. 此方法的最佳位置是您的mvc项目引用的类库项目。 Mainly because that way you can easily reuse it in all your apps. 主要是因为这样您可以轻松地在所有应用中重复使用它。 But also because it is neither Application logic (so doesn't belong in the controller) nor does it belong in the model. 但也因为它既不是应用程序逻辑(因此不属于控制器)也不属于模型。 Some things are just utilities. 有些东西只是公用事业。

Note that to get this to work, the viewPath parameter HAS to be the PHYSICAL PATH to the file, complete with the .aspx extension. 请注意,要使其生效,viewPath参数必须是文件的物理路径,并带有.aspx扩展名。 You can't use a route as WebFormView class requires a physical path in its constructor. 您不能使用路由,因为WebFormView类在其构造函数中需要物理路径。

This will render the full view and take account of the master page. 这将呈现完整视图并考虑母版页。


HEALTH WARNING FOR HTML EMAILS: HTML电子邮件的健康警告:

HTML emails and the devices where you read them are even more difficult and restrictive to design for than websites and browsers. HTML电子邮件和您阅读它们的设备比网站和浏览器更难以设计。 What works in one, will not work in another. 什么在一个工作,什么在另一个工作。 So with html emails, you really have to Keep It Simple! 所以使用HTML电子邮件,你真的必须保持简单! Your lovely page with menus and relative images and whatever else, JUST WON'T WORK in all email devices. 你的可爱页面包含菜单和相关图片以及其他任何内容,只能在所有电子邮件设备中使用。 Just as an example, the images src attribute needs to be absolute and include the domain: 仅作为示例,图像src属性必须是绝对的并包括域:

This won't work: 这不起作用:

<img src="/Images/MyImage.gif" ... /> 

Bit this will: 这将是:

 <img src="http://www.mywebsite.com/Images/MyImage.gif" ... /> 

With those caveats, it works fine and I use it. 有了这些警告,它工作正常,我使用它。 Just don't try to send them the full gimmickry of your website, cos that won't work! 只是不要试图向他们发送你的网站的完整噱头,cos将无法正常工作!

Even more important: 更重要的是:

All CSS must be INLINE and just for basic styling: colours, borders, padding. 所有CSS必须是INLINE并且仅用于基本样式:颜色,边框,填充。 But no floating and positioning. 但没有漂浮和定位。 CSS layouts won't work consistently across devices! CSS布局不能跨设备一致地工作!

You can use MvcView NuGet package to do this! 你可以使用MvcView NuGet包来做到这一点! Its simple, just install using install-package MvcMailer and you are done! 它很简单,只需使用安装包MvcMailer安装即可完成! Use full power of view engine to template, master page, view model and send html emails neatly! 使用视图引擎的全部功能来模板,母版页,查看模型并整齐地发送HTML电子邮件!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM