ASP.NET MVC渲染视图,以通过电子邮件发送的字符串 [英] ASP.NET MVC Render View to a string for emailing

查看:189
本文介绍了ASP.NET MVC渲染视图,以通过电子邮件发送的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用MVC的观点来创建机构的电子邮件,我所遇到的这个(<一个href=\"http://www.brightmix.com/blog/renderpartial-to-string-in-asp-net-mvc/\">http://www.brightmix.com/blog/renderpartial-to-string-in-asp-net-mvc/)但它似乎并没有使用强类型的意见(ViewContext为null)上班。但我的东西后,将使包括母版的完整视图。

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.

任何建议将是伟大的!

先谢谢了。

Gifster

推荐答案

这个问题已经被问(回答)已

The question has been asked (and answered) already:

渲染视图作为字符串

这是我使用了一下:

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项目具有引用的类库项目。主要是因为这样你可以轻松地重用它在所有的应用程序。而且还因为它既不应用逻辑(因此不会在控制器属于)也不在模型中的归属。有些东西都只是工具。

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.

请注意,为了得到这个工作,viewPath参数必须是物理路径的文件,完成与.aspx扩展名。作为WebFormView类需要在其构造的物理路径无法使用的路由。

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.

HTML电子邮件,并在那里你读不懂他们的设备是更加困难和限制性设计比网站和浏览器。什么工程于一体,将无法在另一个工作。因此,与HTML格式的电子邮件,你真的要保持简单!你的菜单和图像的相对和任何其他可爱的页面,只是不会在所有电子邮件的设备。只是作为一个例子,src属性的图像需要是绝对的,包含域

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:

这是行不通的:

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

此位将:

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

通过这些警告,它工作正常,我使用它。只是不要试图给他们你的网站,这是行不通的COS的全部伎俩!

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!

更重要的是:

所有的CSS必须INLINE和公正的基本样式:颜色,边框填充。但是,没有浮动和定位。 CSS布局不会跨设备工作始终!

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!

这篇关于ASP.NET MVC渲染视图,以通过电子邮件发送的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆