I have a System.Web.WebPages.WebPage object I have done some stuff with. Is there a possibility to get the result HTML from the WebPage?
I have searc开发者_运维百科hed at Google, but I couldn't find an answer.
Take a look here. Taken from this blog:
protected string RenderPartialViewToString(string viewName, object model)
{
if (string.IsNullOrEmpty(viewName))
viewName = ControllerContext.RouteData.GetRequiredString("action");
ViewData.Model = model;
using (StringWriter sw = new StringWriter())
{
ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
ViewContext viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);
viewResult.View.Render(viewContext, sw);
return sw.GetStringBuilder().ToString();
}
}
But if you are trying to do what I think that you are trying (render views into string in order to send them as emails) don't do what I showed, simply use MvcMailer or Postal.
You may also find the following blog post useful.
精彩评论