2013-03-14 73 views
1

我試圖讓HTML在我的ASP文本框,但是當我去儘可能單擊它與此錯誤返回按鈕:HTML文本框在錯誤

Uncaught Sys.WebForms.PageRequestManagerServerErrorException: Sys.WebForms.PageRequestManagerServerErrorException: An unknown error occurred while processing the request on the server. The status code returned from the server was: 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)); 
} 
+0

您可以禁用自定義錯誤,以便獲得完整的錯誤消息嗎? – 2013-03-14 18:27:24

+0

你能發佈更多的.aspx代碼嗎?您是否使用UpdatePanels和觸發器? – Rob 2013-03-14 18:35:10

+0

@Knaģis他們已經被禁用。這是在我的本地計算機上進行測試的。 – 2013-03-14 18:35:22

回答

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); 
    } 
} 
+0

它的工作原理!非常感謝您的幫助! – 2013-03-14 19:49:43

1

在你的web.config

嘗試<httpRuntime requestValidationMode="2.0" />隨着ValidateRequest="false"在頁面上。

添加ID="btnSubmit"你按鈕

編輯:

首先,我想Google的原始錯誤。你應該能夠做到這一點。嘗試停止開發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: 確保您AREN不直截了當t sql調用與文本框中的任何東西,你會打開自己打開SQL注入攻擊。這就是爲什麼asp.net首先阻止了「<」。

編輯3 不知道會的問題,而是你的ASP:按鈕沒有ID屬性

+0

我已經在頁面上有ValidateRequest =「false」,我會嘗試httpRuntime。我希望只能在一頁上禁用它。 – 2013-03-14 18:44:01

+0

該網站實際上已經崩潰,我甚至無法使用httpRuntime進行測試。 – 2013-03-14 18:45:59

+0

您必須回退到2.0驗證模式才能運行。默認情況下,4.0會驗證所有請求以防止受到攻擊。 http://www.asp.net/whitepapers/aspnet4/breaking-changes#0.1__Toc256770147 – Rob 2013-03-14 18:46:48