2

I read this very helpful post: how to render html to a string instead of to the screen in MVC3.

I've created a function that does just that in my controller. I would now like to be able to call this method from class in another project so that I can offer this function as a service. The code looks like this:

 using (var sw = new StringWriter())
        {
            var viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
            var viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);
            viewResult.View.Render(viewContext, sw);
            viewResult.ViewEngine.ReleaseView(ControllerContext, viewResult.View);
            return sw.GetStringBuilder().ToString();
        }

I'd like to call this class directly and not via the IIS. But then the ControllerContext is null. Can I specify the View on disc somehow?

Community
  • 1
  • 1
ekenman
  • 995
  • 1
  • 13
  • 29

2 Answers2

0

Most of the controls in MVC have a property called "ToHtmlString();"

JohnnBlade
  • 4,261
  • 1
  • 21
  • 22
0

http://msdn.microsoft.com/en-us/library/system.web.mvc.viewengineresult(v=vs.108)

You can create the ViewEngineResult by providing list of locations instead of calling FindPartialView

Tomas Grosup
  • 6,396
  • 3
  • 30
  • 44