Skip to content

Instantly share code, notes, and snippets.

@jzwang-dev
Last active February 16, 2021 11:45
Show Gist options
  • Save jzwang-dev/844d6f6f213ebfe94af37f1c94d55720 to your computer and use it in GitHub Desktop.
Save jzwang-dev/844d6f6f213ebfe94af37f1c94d55720 to your computer and use it in GitHub Desktop.
using System.IO;
using System.Web.Mvc;
namespace JZLib
{
public static class JZUtil
{
public static string RenderViewToString(ControllerContext controllerContext, string viewName, object viewData = null)
{
using (var writer = new StringWriter())
{
var razorViewEngine = new RazorViewEngine();
var razorViewResult = razorViewEngine.FindView(controllerContext, viewName, "", false);
var viewContext = new ViewContext(controllerContext, razorViewResult.View, new ViewDataDictionary(viewData), new TempDataDictionary(), writer);
razorViewResult.View.Render(viewContext, writer);
return writer.ToString();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment