如何从asp.net aspx页面当前页面的源代码 [英] how to get current page source from asp.net aspx page

查看:102
本文介绍了如何从asp.net aspx页面当前页面的源代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用asp.net应用程序来获得当前页面的源代码。我发现了一张code的它转换HTML到PDF,但为了将我到PDF我需要获得页面的HTML code。我怎样才能得到这些作为一个字符串?我简单的code是这样的:

Hi, I am trying to get the current page source by using the asp.net application. I found a piece of code which converts html to pdf but in order to convert my page to pdf I need to get the html code of the page. How can I get these as a string? My simple code is like this:

        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();

如果我能得到的HTML code关闭我的asp.net页面,我把我的网页上的所有行成
 sbHtml.Append();
通过使用一个for循环,这将解决我的问题,在我看来,code。

If I can get the html code off my asp.net page I put all line of my page into the sbHtml.Append(""); code by using a for loop and this will solve my problem in my opinion.

推荐答案

一种可能性是使用Web客户端发送一个HTTP请求到指定页面,并获得生成的HTML:

One possibility is to use a WebClient to send an HTTP request to the given page and get the resulting HTML:

using (var client = new WebClient())
{
    string html = client.DownloadString("http://example.com/somepage.aspx");
}

这种方法的缺点是,它发送一个额外的HTTP请求。​​

The drawback of this approach is that it sends an additional HTTP request.

另一种可能性是直接渲染web窗体成字符串

Another possibility is to render the WebForm directly into a string:

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

这篇关于如何从asp.net aspx页面当前页面的源代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆