tags:

views:

151

answers:

1

In my ASP.NET MVC application, I would like my controller action to render a view to a string, send an email using that string as the body, then do a redirect.

I've found several articles here on SO and elsewhere on how to render the view to a string. That part was easy.

But when I attempt to perform the redirect, I get an HttpException: Cannot redirect after HTTP headers have been sent. I've tried setting Response.Buffer and Response.BufferOutput to true, I've tried Response.Clear, nothing seems to work.

Is it possible to render a view to a string, then do a redirect?

A: 

I believe you don't have to end the response to render view to a string.

Have a look here and here (this one has the exact sample you need).

The last link should be:

http://mikehadlow.blogspot.com/2008/06/mvc-framework-capturing-output-of-view_05.html

It seems StackOverflow encodes underscore in the link which causes. Just make sure you have _05.html at the end.
Also if I go to the Address bar in the browser (after the link is clicked), put cursor there and hit enter it works.
It is weird.

Dmytrii Nagirniak
Your second link is dead.
JohnRudolfLewis
Updated the answer.
Dmytrii Nagirniak