渲染上即时视图 [英] Rendering a view on-the-fly
问题描述
我开发一个ASP.NET MVC应用程序,它向用户发送确认电子邮件。对于电子邮件本身,我想创建一个视图,然后渲染视图,并使用.NET对象的邮件发送。
I'm developing an ASP.NET MVC application that will send the user a confirmation email. For the email itself, I'd like to create a view and then render that view and send it using the .NET mail objects.
我怎样才能做到这一点使用MVC框架?
How can I do this using the MVC framework?
推荐答案
按我的意见,这code没有工作,但它总是导致一个HTTP标头后不能重定向已发送错误
As per my comment on Richard's answer, this code did work, but it always resulted in a 'Cannot redirect after HTTP headers have been sent' error.
后的很多的周围挖谷歌正在和沮丧的,我终于找到了一些code,似乎这样的伎俩,这篇文章:
After a lot of digging around Google and being frustrated, I finally found some code that seems to do the trick, on this article:
的http://mikehadlow.blogspot.com/2008/06/mvc-framework-capturing-output-of-view%5F05.html
这家伙的方法是创建自己的HttpContext。
This guy's method is to create his own HttpContext.
而不是使用MVCContrib BlockRenderer我只需更换当前HttpContext用新的承载写入到一个StringWriter的一个响应。
Rather than use the MVCContrib BlockRenderer I simply replace the current HttpContext with a new one that hosts a Response that writes to a StringWriter.
此方法效果很好(未成年人不同的是,我要创建渲染我的部分观点的单独行动,但没有戏剧那里)。
This method works perfectly (a minor difference is that I had to create a separate Action for rendering my partial view, but no drama there).
这篇关于渲染上即时视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!