Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers 2

up vote 1 down vote accepted

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.

share|improve this answer
1  
will a mocked context work here? –  Simon_Weaver Jan 13 '10 at 22:04
    
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 Jan 13 '10 at 22:05
    
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 Jan 15 '10 at 1:17
add comment

Why not create a ControllerContext then, even if fake?

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.