Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How get html of current page?

Tags:

asp.net

I want parse the html of current page. How can I get the html of current page for that in asp.net?

Thanks in advance.

like image 720
Constantine Avatar asked Mar 01 '23 04:03

Constantine


1 Answers

for client side

In Internet explorer

Right click on the browser --> View source

IN firefox

Right click on the browser --> View Page Source

for server side

You can override the page's render method to capture the HTML source on the server-side.

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
}
like image 116
solairaja Avatar answered Mar 12 '23 12:03

solairaja