Is their a solution to generate an email template using an ASP.NET MVC View without having to jump through hoops.
Let me elaborate jumping through hoops.
var fakeContext = new HttpContext(HttpContext.Current.Request, fakeResponse);
var oldContext = HttpContext.Current;
HttpContext.Current = fakeContext;
var html = new HtmlHelper(new ViewContext(fakeControllerContext,
new FakeView(), viewDataDictionary, new TempDataDictionary()),
new ViewPage());
html.RenderPartial(viewName, viewData, viewDataDictionary);
HttpContext.Current = oldContext;
The above code is using the current HttpContext to fake a new Context and render the page with RenderPartial, we shouldn't have to do this.
Another very detailed solution using ControllerContext and .Render: (IEmailTemplateService, Headers/Postback WorkAround) but pretty much doing the same thing with a lot more code.
I on the other hand, am looking for something that would just render a View without having to POST/GET and generates me a simple string that I can send off through my Email code. Something that doesn't run into errors such as posting headers twice or faking some piece of data.
EX:
//code which does not fire Render, RenderPartial... etc
var email = emailFramework.Create(viewData, view);
See my solution bellow or follow this link:
My Solution using spark: (12/30/2009) ASP.NET MVC Email Template Solution
This is what I wanted the ASP.NET MVC ViewEngine to do, but it's in Spark, just follow the latest link right bellow,
Update (12/30/2009) Cleaner Version: ASP.NET MVC Email Template Solution
(11/16/2009) Or, Louis DeJardin Console Application Version:
I decided to use this method because it seems to be the one that does everything right, it:
Please, make sure to read these posts. All credit to Louis DeJardin see his tutorials :): Using Spark as a general purpose template engine!, Email Templates Revisited
Why do you need to create the email from a view? Why not use a plain old template file? I do this all the time - I make a template and use the NVelocity engine from the castle project (not to be confused with an nvelocity VIEW engine) to render the template.
Example:
The NVelocityEngine class is a wrapper I wrote around the NVelocity port provided by the Castle project as shown below:
In this way, you don't have to meddle with the view engine at all, and you can theoretically chain this with the ASP.NET view engine if you wanted, like I have done in the following controller method:
You can consider using MvcMailer NuGet - it does just what you are looking for and does it cleanly. See the NuGet package here and the project documentation
Hope it helps!
Try using spark view engine (http://www.sparkviewengine.com/). It is easy to use, nicer than standard engine and doesn't require to fake context.
You can also use function from this answer Render a view as a string , but it requires faking context. This is the way standard view engine works and you can do nothing about that.
This is my extension class that is used to generate views to string. First is for standard view engine, second for Spark:
I created an overload to LukLed's RenderSparkToString method that allows you to use a spark layout along with your view:
I agree with Andrew though. I wish there was an easier way to do this with the web forms view engine.
Although this is a little old thread, I would encourage if you to take a look at the MvcMailer NuGet package - it simplifies the whole thing greatly and makes the mailer behave li