Created
March 23, 2011 14:32
-
-
Save SergXIIIth/883183 to your computer and use it in GitHub Desktop.
asp mvc view to string
This file contains hidden or 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; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Web.Mvc; | |
using System.Web.Mvc.Html; | |
using System.Web; | |
using System.IO; | |
namespace Msa.Common | |
{ | |
public static string RenderPartialViewToString(this Controller controller, string viewName, object model) | |
{ | |
if (string.IsNullOrEmpty(viewName)) | |
{ | |
viewName = controller.ControllerContext.RouteData.GetRequiredString("action"); | |
} | |
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); | |
return sw.GetStringBuilder().ToString(); | |
} | |
} | |
/// <summary> | |
/// http://stackoverflow.com/questions/483091/render-a-view-as-a-string | |
/// or http://www.klopfenstein.net/lorenz.aspx/render-partial-view-to-string-in-asp-net-mvc | |
/// </summary> | |
/// <remarks> | |
/// use it in controller this.RenderViewToString("View", Model) | |
/// </remarks> | |
public static class ViewToString | |
{ | |
/// <summary>Renders a view to string.</summary> | |
public static string RenderPartialToString(this HtmlHelper html, string viewName, object viewData) | |
{ | |
return RenderViewToString(html.ViewContext.Controller.ControllerContext, viewName, viewData); | |
} | |
/// <summary>Renders a view to string.</summary> | |
public static string RenderViewToString(this Controller controller, string viewName, object viewData) | |
{ | |
return RenderViewToString(controller.ControllerContext, viewName, viewData); | |
} | |
private static string RenderViewToString(ControllerContext context, | |
string viewName, object viewData) | |
{ | |
//Create memory writer | |
var sb = new StringBuilder(); | |
var memWriter = new StringWriter(sb); | |
//Create fake http context to render the view | |
var fakeResponse = new HttpResponse(memWriter); | |
var fakeContext = new HttpContext(HttpContext.Current.Request, fakeResponse); | |
var fakeControllerContext = new ControllerContext( | |
new HttpContextWrapper(fakeContext), | |
context.RouteData, context.Controller); | |
var oldContext = HttpContext.Current; | |
HttpContext.Current = fakeContext; | |
//Use HtmlHelper to render partial view to fake context | |
var html = new HtmlHelper(new ViewContext(fakeControllerContext, | |
new FakeView(), new ViewDataDictionary(), new TempDataDictionary(), memWriter), | |
new ViewPage()); | |
html.RenderPartial(viewName, viewData); | |
//Restore context | |
HttpContext.Current = oldContext; | |
//Flush memory and return output | |
memWriter.Flush(); | |
return sb.ToString(); | |
} | |
/// <summary>Fake IView implementation, only used to instantiate an HtmlHelper.</summary> | |
public class FakeView : IView | |
{ | |
#region IView Members | |
public void Render(ViewContext viewContext, System.IO.TextWriter writer) | |
{ | |
throw new NotImplementedException(); | |
} | |
#endregion | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment