5

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?

1
  • i'm looking for the same thing. Commented Apr 8, 2013 at 12:31

3 Answers 3

3

BuildStarted and I have put together a standalone Razor templating engine which you might find useful (Github). You'll need to adapt your .ascx content into a simple string template, but it should do the job.

If you've got NuGet, you can run Install-Package RazorEngine

Sign up to request clarification or add additional context in comments.

Comments

2

You may checkout the following blog post. Also Postal is worth looking at.

Comments

1

You need to create a fake HttpContextBase with a fake HttpRequestBase that return meaningful values from their properties.

2 Comments

Could you please be more specific, I mean how can I do that?
@Florin: Create classes that inherit HttpContextBase and HttpRequestBase and override the basic properties. You can then use them to create a ControllerContext.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.