[英]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:只需将以下内容添加到页面:
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.