简体   繁体   English

如何从代码后面获取html页面的渲染源代码,以便我可以通过邮件发送它

[英]How to get rendered source code of an html page from code behind so I can send it in mail

I created an aspx page wit a button send. 我用按钮发送创建了一个aspx页面。 on that button click a function code behind goes on. 在该按钮上单击后面的功能代码继续。 What I want is that when I click on the button to get all the html source code rendered of the aspx page in a variable so I can send it in a mail. 我想要的是,当我点击按钮获取变量中的aspx页面的所有html源代码时,我可以通过邮件发送它。 how can I get the rendered html source code from code behind function. 如何从函数后面的代码中获取渲染的html源代码。

I actually just wrote a method for this not too long ago. 我实际上刚刚在不久前为此写了一个方法。 It gets the view at a provided relative path, passes in a provided model, and renders the view as a string. 它在提供的相对路径下获取视图,传入提供的模型,并将视图呈现为字符串。

public static string RenderViewToString(string relativePathToControl, object viewData)
{
    ViewPage viewPage = new ViewPage() { ViewContext = new ViewContext() };

    viewPage.ViewData = new ViewDataDictionary(viewData);
    viewPage.Controls.Add(viewPage.LoadControl(relativePathToControl));

    StringBuilder sb = new StringBuilder();
    using (StringWriter sw = new StringWriter(sb))
    {
        using (HtmlTextWriter tw = new HtmlTextWriter(sw))
        {
            viewPage.RenderControl(tw);
        }
    }

    return sb.ToString();
}

You may need to add a few using statements. 您可能需要添加一些using语句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM