ASP.NET MVC 将视图渲染为用于电子邮件发送的字符串

发布于 2024-10-02 06:23:33 字数 382 浏览 5 评论 0原文

我想使用 MVC 视图创建电子邮件正文,我遇到过这个(http://www.brightmix.com/blog/renderpartial-to-string-in-asp-net-mvc/),但它似乎不适用于强类型视图(ViewContext 为空)。但我想要的是能够呈现包括母版页在内的完整视图的东西。

我认为,如果有一种方法只需调用视图而不进行重定向,只需写入不同的流并在控制器内发送电子邮件就可以做到这一点,但我不知道如何调用视图。

任何建议都会很棒!

提前致谢。

礼品商

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). 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凌乱心跳 2024-10-09 06:23:33

这个问题已经被提出(并回答):

将视图渲染为字符串

这是我使用的部分:

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();
        }
    }

此方法的最佳位置是在您的 mvc 项目引用的类库项目中。主要是因为这样您可以轻松地在所有应用程序中重用它。但也因为它既不是应用程序逻辑(因此不属于控制器)也不属于模型。有些东西只是实用程序。

请注意,要使其正常工作,viewPath 参数必须是文件的物理路径,并带有 .aspx 扩展名。您不能使用路由,因为 WebFormView 类需要在其构造函数中使用物理路径。

这将呈现完整视图并考虑母版页。


HTML 电子邮件的健康警告:

HTML 电子邮件和阅读它们的设备比网站和浏览器的设计更加困难和限制性更大。在一种情况下有效的方法,在另一种情况下则无效。因此,对于 html 电子邮件,您确实必须保持简单!您带有菜单和相关图像以及其他任何内容的可爱页面,只是不适用于所有电子邮件设备。举个例子,images src 属性需要是绝对的并包含域:

这不会工作:

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

位这会:

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

有了这些警告,它工作正常,我使用它。只是不要试图向他们发送您网站的全部花招,因为那是行不通的!

更重要的是:

所有 CSS 都必须是内联的,并且仅用于基本样式:颜色、边框、填充。但没有浮动和定位。 CSS 布局在不同设备上的工作方式不一致!

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. 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. You can't use a route as WebFormView class requires a physical path in its constructor.

This will render the full view and take account of the master page.


HEALTH WARNING FOR HTML EMAILS:

HTML emails and the devices where you read them are even more difficult and restrictive to design for than websites and browsers. What works in one, will not work in another. So with html emails, you really have to Keep It Simple! 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:

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!

Even more important:

All CSS must be INLINE and just for basic styling: colours, borders, padding. But no floating and positioning. CSS layouts won't work consistently across devices!

開玄 2024-10-09 06:23:33

您可以使用 MvcView NuGet 包来执行此操作!很简单,只需使用安装包 MvcMailer 安装即可!使用视图引擎的全部功能来灵活地创建模板、母版页、视图模型并发送 html 电子邮件!

You can use MvcView NuGet package to do this! Its simple, just install using install-package MvcMailer and you are done! Use full power of view engine to template, master page, view model and send html emails neatly!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文