开发者

Get result HTML from WebPage

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜