加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – 如何获取当前页面的HTML?

发布时间:2020-12-16 06:35:40 所属栏目:asp.Net 来源:网络整理
导读:我想解析当前页面的html. 如何在asp.net中获取当前页面的html? 提前致谢. 解决方法 对于客户方 在Internet Explorer中 右键单击浏览器 – 查看来源 在Firefox中 右键单击浏览器 – 查看页面源代码 对于服务器端 您可以覆盖页面的render方法以捕获服务器端的
我想解析当前页面的html.
如何在asp.net中获取当前页面的html?

提前致谢.

解决方法

对于客户方

在Internet Explorer中

右键单击浏览器 – >查看来源

在Firefox中

右键单击浏览器 – >查看页面源代码

对于服务器端

您可以覆盖页面的render方法以捕获服务器端的HTML源代码.

protected override void Render(HtmlTextWriter writer)
{
    // setup a TextWriter to capture the markup
    TextWriter tw = new StringWriter();
    HtmlTextWriter htw = new HtmlTextWriter(tw);

    // render the markup into our surrogate TextWriter
    base.Render(htw);

    // get the captured markup as a string
    string pageSource = tw.ToString();

    // render the markup into the output stream verbatim
    writer.Write(pageSource);

    // remove the viewstate field from the captured markup
    string viewStateRemoved = Regex.Replace(pageSource,"<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value=".*?" />","",RegexOptions.IgnoreCase);

    // the page source,without the viewstate field,is in viewStateRemoved
    // do what you like with it
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读