Last active
February 16, 2021 11:45
-
-
Save jzwang-dev/844d6f6f213ebfe94af37f1c94d55720 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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