1

我试图在我的 asp 文本框中允许 HTML,但是当我点击按钮时,它会返回此错误:

未捕获的 Sys.WebForms.PageRequestManagerServerErrorException:Sys.WebForms.PageRequestManagerServerErrorException:处理服务器上的请求时发生未知错误。从服务器返回的状态码是:404。”

Server.HtmlEncode用来在文本框中对 HTML 进行编码但仍然没有结果的服务器端之一。我尝试使用断点 , AutoEventWireup="false"ValidateRequest="false"并在 web.config 中将页面验证设置为 false。我也尝试ValidateRequest="false"在页面上使用它自己。它也返回这个错误A potentially dangerous Request.Form value was detected from the client (ctl00$ContentPlaceHolder1$testBox="<test")。服务器端和客户端代码如下:

--Client side--
<asp:TextBox runat="server" ID="testBox" />
<asp:Button runat="server" OnClick="testHtmlEncode" />

--Server side--
protected void testHtmlEncode(Object sender, EventArgs e) {
console.write(Server.HtmlEncode(testBox.Text));
}
4

2 回答 2

1

我让它与这个aspx一起工作:

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" ValidateRequest="false" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:TextBox runat="server" ID="txtValue"></asp:TextBox>
        <asp:Button runat="server" ID="btnSubmit" OnClick="btnSubmit_Click" Text="submit"/>
    </div>
    </form>
</body>
</html>

这个 web.config

<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.5"/>
    <httpRuntime targetFramework="4.5" requestValidationMode="2.0"/>
  </system.web>
</configuration>

这个 .aspx.cs

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        Console.WriteLine(txtValue.Text);
    }
}
于 2013-03-14T19:38:49.150 回答
1

试试<httpRuntime requestValidationMode="2.0" />你的 web.config

连同ValidateRequest="false"在页面上。

添加ID="btnSubmit"给你按钮

编辑:

首先,我会谷歌你原来的错误。你应该能够做到这一点。尝试停止开发 Web 服务器并重新运行(任务栏中的图标)。

https://www.google.com/?q=A+potentially+dangerous+Request.Form+value+was+detected+from+the+client

对于 jQuery 解决方案:

在 .aspx.cs 中

[WebMethod]
  public static void MyMethod(string myTextBoxValue)
  {
    //do something
  }

在 .aspx 中

var txtValue= $('#<%=testBox.ClientID%>').val();
        if (!txtValue) {
            txtValue= 0;
        }
        var objJSON = {
            myTextBoxValue: txtValue
        };
        var encoded = JSON.stringify(objJSON);

$.ajax({
  type: "POST",
  url: "PageName.aspx/MyMethod",
  data: encoded,
  contentType: "application/json; charset=utf-8",
  dataType: "json",
  success: function(msg) {
    // Do something interesting here.
  }
});

编辑 2: 确保您没有使用文本框中的任何内容进行直接的 sql 调用,您将打开自己的 sql 注入攻击。这就是为什么 asp.net 首先阻止了“<”。

编辑 3 不确定是否重要,但您的 asp:Button 没有 ID 属性

于 2013-03-14T18:43:01.000 回答