首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何获取当前页面的html?

问如何获取当前页面的html?
EN

Stack Overflow用户
提问于 2009-10-19 12:33:01
回答 3查看 11.9K关注 0票数 4

我想解析当前页面的html。如何在asp.net中获取当前页面的html?

提前谢谢。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2009-10-19 12:36:35

客户端的

在Internet explorer中

右键单击浏览器-->查看源代码

在firefox中

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

服务器端的

您可以重写页面的render方法来捕获服务器端的HTML源代码。

代码语言:javascript
复制
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
}
票数 5
EN

Stack Overflow用户

发布于 2009-10-19 12:35:48

覆盖Render方法并使用您自己的HtmlWriter调用base.Render。

票数 2
EN

Stack Overflow用户

发布于 2009-10-19 12:52:07

您真的想要解析HTML吗?这是一件棘手的事情。如果您不一定要这样做,我会通过在客户端使用DOM方法来避免这种情况(如果客户端解决方案是可接受的)。如果您经常这样做,您可以考虑使用jQuery、Prototype或其他工具来提供帮助。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1588431

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档