简体   繁体   English

将 ASPX 页面转换为字符串,而不会触发“需要放置在带有 runat="server" 消息的表单中

[英]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.我正在尝试抓取我自己的 ASPX 页面,以便将其输入 HTML Agility Pack 解析器。 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.我有时会得到“控件必须在带有 runat=server 的表单内”。 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.我需要尝试解决我的问题,无论是让 HTML 敏捷包解析器以另一种方式工作,还是将代码转换为没有错误的字符串。 It doesn't matter - I just need to get things working.没关系 - 我只需要让事情正常进行。

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" EnableEventValidation="假"
  2. public override void VerifyRenderingInServerForm(Control control) {}公共覆盖无效验证RenderingInServerForm(控制控制){}

Here's what Microsoft have to say about disabling event validation:以下是 Microsoft 关于禁用事件验证的说法:

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.但是,在不回发的页面上使用它是安全的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从aspx页面提交表单(没有runat = server) - Submit form (without runat=server) from aspx page 必须放置在带有 runat=server 的表单标签内 - Must be Placed Inside a Form Tag With runat=server 我的显示“ TextBox”的ASP.NET登录页面必须放在带有runat = server的表单标记中 - My ASP.NET login page showing 'TextBox' must be placed inside a form tag with runat=server 类型[…]的控件[…]必须放置在带有runat = server的表单标记中-但已经是 - Control […] of type […] must be placed inside a form tag with runat=server - but already is 类型为'TextBox'的控件'0'必须放置在带有runat = server的表单标记中 - Control '0' of type 'TextBox' must be placed inside a form tag with runat=server 类型为“ HtmlEditorExtender”的控件必须放在带有runat = server的表单标签中 - Control of type 'HtmlEditorExtender' must be placed inside a form tag with runat=server “ ImageButton”必须放置在带有runat = server的表单标签中 - 'ImageButton' must be placed inside a form tag with runat=server 收到错误“必须放置在具有 runat=server 的表单标记内”,但它是 - getting error 'must be placed inside a form tag with runat=server', but it is 在服务器上运行的表单内的Gridview引发““ GridView”必须放置在带有runat = server的表单标记内” - Gridview inside of form running at server throws “'GridView' must be placed inside aform tag with runat=server” 自定义表单控件中的DropDownList给出了“ DropDownList”必须放在带有runat = server的表单标签中, - DropDownList in a custom form control gives 'DropDownList' must be placed inside a form tag with runat=server,
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM