Created
April 29, 2016 13:21
-
-
Save dataneek/89401c66cc095bbbafb82e5d8383b106 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 ControllerExtensions | |
{ | |
public static string RenderRazorViewToString(this Controller c, string viewName, object model) | |
{ | |
c.ViewData.Model = model; | |
using (var sw = new StringWriter()) | |
{ | |
var viewResult = ViewEngines.Engines.FindPartialView(c.ControllerContext, viewName); | |
var viewContext = new ViewContext(c.ControllerContext, viewResult.View, c.ViewData, c.TempData, sw); | |
viewResult.View.Render(viewContext, sw); | |
viewResult.ViewEngine.ReleaseView(c.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