Render view to string followed by redirect results in exception

一曲冷凌霜 提交于 2019-12-04 15:12:46

You'd have to initiate a new request. Bit, do you really want to send emails synchronously this way? If the mail server is down, the user could be waiting a good while.

I always put emails in an offline queue and have a service mail them. You might consider using the Spark template engine for this.

One other approach is to not redirect but write out a page with a meta redirect tag

Here is an alternative method for rendering a view to a string that never results in data being output to the response (therefore it should avoid your problem): http://craftycodeblog.com/2010/05/15/asp-net-mvc-render-partial-view-to-string/

To render a regular view instead of a partial view, you'll need to change "ViewEngines.Engines.FindPartialView" to "ViewEngines.Engines.FindView".

Have a look at the MVC Contrib EmailTemplateService which does exactly what you are after.

http://mvccontrib.googlecode.com/svn/trunk/src/MVCContrib/Services/EmailTemplateService.cs

Sorry Chris, not quite sure what I was thinking but I obviously didn't read the question. While I cannot give you a way around this, I can tell you why you are getting the error - HttpResponse.Flush() sends the headers before flushing the content to your filter. This sets a flag inside the response so that when you try to redirect you get the exception.

Using reflector to look at the code inside Flush, I can't see any clean way for you to get around this without a lot of reflection and other nastiness.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!