Skip to content

Instantly share code, notes, and snippets.

@XEngine
Created June 7, 2019 06:44
Show Gist options
  • Save XEngine/1f43c300e1cf26fb64638f92ab36da18 to your computer and use it in GitHub Desktop.
Save XEngine/1f43c300e1cf26fb64638f92ab36da18 to your computer and use it in GitHub Desktop.
public static class RazorProviderExtensions
{
public static string RenderRazorViewToString(this Controller controller, string viewName, object model = null)
{
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