asp.net - 如何获取当前页面的html?

标签 asp.net

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

提前致谢。

最佳答案

客户端

在 Internet Explorer 中

浏览器右键-->查看源码

在火狐

浏览器右键-->查看页面源

用于服务器端

您可以覆盖页面的渲染方法以在服务器端捕获 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
}

关于asp.net - 如何获取当前页面的html?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1588431/

相关文章:

c# - 将文本框/标签中的文本颜色设置为红色并使其变为粗体

asp.net - Npgsql - 完全连接阻塞

c# - 以编程方式将按钮移动到另一个 tr

c# - ASP.NET Identity 的默认密码哈希器 - 它如何工作并且安全吗?

c# - 使用 ASP.NET 2.0/C# 处理错误报告方法

asp.net - IIS-无法获取必需的权限。已启用“Full Trust”,并且在应用程序池上加载用户配置文件是“True”

c# - 在 C# 中呈现 MediaWiki 的最佳方法?

c# - 如何将 gridview 中的货币舍入到有效数字?

asp.net - 用于 ASP.NET 的 web api - 如何构建对象流

c# - 单击 jqGrid 中的一行获取列的值