asp.netwebformshtml-agility-packhtmltextwriter

Convert ASPX page to string without triggering the 'needs to be placed inside a form with a runat="server"' message


I'm trying to scrape my own ASPX page so that I can feed it into HTML Agility Pack parser. I've tried all ways and a string is the only thing I can get to work in this instance.

I'm using the following code to turn an outer control into a string:

    static string ConvertControlToString(Control ctl)
    {
        string s = null;

        var sw = new StringWriter();
        using (var w = new HtmlTextWriter(sw))
        {
            ctl.RenderControl(w);
            s = sw.ToString();
        }
        return s;
    }

The concept works, except for some annoying glitches. I get the "control must be inside a form with a runat=server" on occasions. It seems to be triggered by controls that cause postback - buttons, update panels etc.

To be clear, my page is in a form, so that's not the issue.

I need to try and work out a solution to my problem, whether that's getting the HTML agility pack parser to work in another way, or to convert the code to a string without errors. It doesn't matter - I just need to get things working.


Solution

  • After lots of trial an error, I've found a solution that works - in that it fixes the problem. However, it also introduces potential security issues, so you need to be wary about how it is used.

    Simply add the following to the page:

    1. EnableEventValidation="false"
    2. public override void VerifyRenderingInServerForm(Control control) {}

    Here's what Microsoft have to say about disabling event validation:

    This feature reduces the risk of unauthorized or malicious postback requests and callbacks. It is strongly recommended that you do not disable event validation.

    However, it is safe to use on pages that don't postback.