Created
June 7, 2019 06:44
-
-
Save XEngine/1f43c300e1cf26fb64638f92ab36da18 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
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