Skip to content

Instantly share code, notes, and snippets.

@dataneek
Created April 29, 2016 13:21
Show Gist options
  • Save dataneek/89401c66cc095bbbafb82e5d8383b106 to your computer and use it in GitHub Desktop.
Save dataneek/89401c66cc095bbbafb82e5d8383b106 to your computer and use it in GitHub Desktop.
public static class ControllerExtensions
{
public static string RenderRazorViewToString(this Controller c, string viewName, object model)
{
c.ViewData.Model = model;
using (var sw = new StringWriter())
{
var viewResult = ViewEngines.Engines.FindPartialView(c.ControllerContext, viewName);
var viewContext = new ViewContext(c.ControllerContext, viewResult.View, c.ViewData, c.TempData, sw);
viewResult.View.Render(viewContext, sw);
viewResult.ViewEngine.ReleaseView(c.ControllerContext, viewResult.View);
return sw.GetStringBuilder().ToString();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment