Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save warrendodsworth/b15562d441a04985d165828f023ca30e to your computer and use it in GitHub Desktop.

Select an option

Save warrendodsworth/b15562d441a04985d165828f023ca30e to your computer and use it in GitHub Desktop.
How to render razor view to string
protected string RenderRazorViewToString(string viewName, object model)
{
ViewData.Model = model;
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();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment