views:

548

answers:

2

I need to render an ASP.NET MVC view to a string so as to be able to send it via an email (it is an order confirmation email defined in an .ascx file ).

I've successfully been able to render an ASP.NET MVC View to string using one of the methods in this question.

However now I need to be able to do it via a WCF service (that will be accessed via silverlight) and so I don't have a ControllerContext. This WCF service is contained within the same project as my MVC project so has access to all my models etc.

I've looked at several questions on Stackoverflow about this issue, but they all seem to need a controller context. I thought there was something in mvccontrib but it doesn't seem to be there anymore.

The closest I've found is the accepted answer to the aforementioned question, but it unfortunately breaks with RenderPartial within the view you're rendering.

I'm hoping maybe some of the behind the scenes work for ASP.NET MVC 2 related to RenderAction may help make this possible now?

A: 

Why not create a ControllerContext then, even if fake?

Jess
+1  A: 

The way that the web forms view engine is integrated into MVC requires the controller context as it actually bubbles the templating / rendering up to the ASP.NET Page class which writes the template content directly to the response stream.

I suggest you take a look at the spark view engine (which will render WFVE templates without change) and use that to generate the templated emails from within the WCF service. There are examples of this in the Spark download.

Neal
will a mocked context work here?
Simon_Weaver
or is that only useful for running a controller action and retrieving a model out? i've tried before to mock a context and didn't get very far. right now i just dont have time to play with something else
Simon_Weaver
I wouldn't recommend mocking the context as unless you essentially recreate a perfect mock you risk introducing bugs. Seriously, spark took me less than an hour to integrate it's worth a look.
Neal