Skip to content

Instantly share code, notes, and snippets.

Created May 29, 2012 15:15
What would you like to do?
RenderRazorViewToString extension
// credit:
public static string RenderRazorViewToString(this Controller controller, string viewName, object model)
controller.ViewData.Model = model;
using (var sw = new StringWriter())
var viewResult = ViewEngines.Engines.FindPartialView(controller.ControllerContext, viewName);
var viewContext = new ViewContext(controller.ControllerContext, viewResult.View, controller.ViewData, controller.TempData, sw);
viewResult.View.Render(viewContext, sw);
viewResult.ViewEngine.ReleaseView(controller.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