Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从asp.net aspx页面获取当前页面源_C#_Asp.net_Html - Fatal编程技术网

C# 如何从asp.net aspx页面获取当前页面源

C# 如何从asp.net aspx页面获取当前页面源,c#,asp.net,html,C#,Asp.net,Html,您好,我正在尝试使用asp.net应用程序获取当前页面源代码。我发现了一段将html转换为pdf的代码,但为了将我的页面转换为pdf,我需要获取页面的html代码。我怎样才能把这些串起来?我的简单代码如下: string sPathToWritePdfTo = Server.MapPath("") + "/pdf_dosya_adi.pdf"; System.Text.StringBuilder sbHtml = new System.Text.StringBu

您好,我正在尝试使用asp.net应用程序获取当前页面源代码。我发现了一段将html转换为pdf的代码,但为了将我的页面转换为pdf,我需要获取页面的html代码。我怎样才能把这些串起来?我的简单代码如下:

        string sPathToWritePdfTo = Server.MapPath("") + "/pdf_dosya_adi.pdf";

        System.Text.StringBuilder sbHtml = new System.Text.StringBuilder();
        sbHtml.Append("<html>");
        sbHtml.Append("<body>");
        sbHtml.Append("<font size='14'>HTML den PDF çevirme Test</font>");
        sbHtml.Append("<br />");
        sbHtml.Append("Body kısmında yazacak yazı");
        sbHtml.Append("</body>");
        sbHtml.Append("</html>");

        using (System.IO.Stream stream = new System.IO.FileStream

        (sPathToWritePdfTo, System.IO.FileMode.OpenOrCreate))
        {
            Pdfizer.HtmlToPdfConverter htmlToPdf = new Pdfizer.HtmlToPdfConverter();
            htmlToPdf.Open(stream);
            htmlToPdf.Run(sbHtml.ToString());
            htmlToPdf.Close();
        }
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "friendlypdfname.pdf"));
        HttpContext.Current.Response.ContentType = "application/pdf";

        HttpContext.Current.Response.WriteFile(sPathToWritePdfTo);
        HttpContext.Current.Response.End();
string sPathToWritePdfTo=Server.MapPath(“”+“/pdf\u dosya\u adi.pdf”;
System.Text.StringBuilder sbHtml=新建System.Text.StringBuilder();
sbHtml.Append(“”);
sbHtml.Append(“”);
追加(“HTML den PDFçevirme Test”);
sbHtml.Append(“
”); 附加(“正文kısmında yazacak yazı”); sbHtml.Append(“”); sbHtml.Append(“”); 使用(System.IO.Stream=new System.IO.FileStream (sPathToWritePdfTo,System.IO.FileMode.OpenOrCreate) { Pdfizer.HtmlToPdfConverter htmlToPdf=新Pdfizer.HtmlToPdfConverter(); htmlToPdf.Open(stream); htmlToPdf.Run(sbHtml.ToString()); htmlToPdf.Close(); } HttpContext.Current.Response.Clear(); HttpContext.Current.Response.AddHeader(“内容处置”,string.Format(“附件;文件名={0}”,“friendlypdfname.pdf”); HttpContext.Current.Response.ContentType=“application/pdf”; HttpContext.Current.Response.WriteFile(sPathToWritePdfTo); HttpContext.Current.Response.End();
如果我能从asp.net页面中获取html代码,我会将页面的所有行放入 sbHtml.Append(“”);
通过使用for循环进行编码,我认为这将解决我的问题。

一种可能性是使用WebClient向给定页面发送HTTP请求,并获取生成的HTML:

using (var client = new WebClient())
{
    string html = client.DownloadString("http://example.com/somepage.aspx");
}
这种方法的缺点是它发送额外的HTTP请求

另一种可能是将WebForm直接呈现为字符串:

using (var writer = new StringWriter())
{
    Server.Execute("SomePage.aspx", writer);
    string html = writer.GetStringBuilder().ToString();
}

您可以创建一个隐藏字段,将当前html添加到其中,并在asyc回发中从事件中检索该字段。假设您正在呈现一个页面,可能正在编辑或更改数据,然后单击按钮下载pdf

//隐藏输入字段

<input type="hidden" runat="server" id="hdn_container" /> 

确保为脚本管理器设置适当的AsyncPostBackTimeout。在您的webconfig中,输入适当的maxRequestLength和executionTimeout。

@EmreAltun,如果您想使用WebClient,您需要指定网页的完整地址。我使用此代码,但它总是再次调用我的当前页面,并导致循环。@EmreAltun,您应该将生成PDF的代码放入某个单击处理程序或其他程序中。不在页面加载中。很明显,如果你把它放在页面加载中,它会导致一个无限循环。你必须先有一个HTML版本的页面。创意+1,但如果他试图获得同一页面的源呢?有了这两个想法,他崩溃了页面,因为他将一次又一次地运行同一页面。我认为他必须在url上添加一个标志,以便在问题来自页面内部时不再重新生成。是的,我想做的是获取同一页面的源代码,这样我就无法摆脱无限循环。有什么解决方案吗?
<asp:Button ID="btnDownload" runat="server" OnClientClick="refreshHtml();" OnClick="btnDownloadButton_Click" Text="Download Pdf"></asp:Button>
<script language="javascript" type="text/javascript">
        function refreshHtml() {
           document.getElementById('<%= hdn_container.ClientID %>').value = document.head.innerHTML + document.body.innerHTML;
        }
</script>
hdn_container.Value