2015-03-25 35 views
0

Webform1.aspx has grid.Grid contains edit button。點擊編輯按鈕,用戶被重定向到webform2.aspx頁面。查詢字符串ID傳遞給Webform2.aspxWebform2.aspx顯示該ID的所有數據。 Webform包含下拉列表,文本框和按鈕等。從客戶端檢測到潛在危險的Request.Form值

我從數據庫中提取記錄並將其分配給像下拉列表,文本框和按鈕之類的控件。將值分配給包含<>的文本框並嘗試更新記錄時。它引發一個異常。例外是A potentially dangerous Request.Form value was detected from the client。所以我嘗試使用htmlencode方法。

下面是我的代碼 在CS文件

public partial class Webform1 : System.Web.UI.Page 
    { 
    string strUrl=""; 
    protected void Page_Load(object sender, EventArgs e) 
    { 

    // Fetching value from database and assigning to string 
    Textbox1.Text= dr["URL"].ToString(); 
    // directly use string 
    //strUrl= dr["URL"].ToString(); 
    } 

    protected void button_Click(object sender, EventArgs e) 
    { 
    string s2 =""; 
    string s3 = "";    
    strUrl= Textbox1.Text;     
     if ((strUrl.Contains("<")) || (strUrl.Contains(">"))) 
     { 
      s2 = Server.HtmlEncode("<"); 
      s3 = Server.HtmlEncode(">"); 
      strUrl= strPingUrl.Replace("<", s2); 
      strUrl= strPingUrl.Replace(">", s3);          
     } 
    Textbox1.Text =strUrl; 
    // updation code 

當我將嘗試插入它拋出分配值的TextBox如果我使用string.It工作的exception.Instead。我可以更新記錄。但我不想這樣。用戶可以更改文本框中的值。所以我想從文本框中獲取價值。

+0

你檢查嗎? http://stackoverflow.com/questions/81991/a-potentially-dangerous-request-form-value-was-detected-from-the-client?rq=1 – Ravimallya 2015-03-25 05:34:12

+0

是的,我訪問該頁面。我不想使用這段代碼。 <的httpRuntime requestValidationMode = 「2.0」/> <結構> <頁validateRequest = 「假」/> 2015-03-25 05:39:00

+0

嘗試http://www.codeproject.com/Tips/423413/Submit -HTML-Text-with-Page-Validation-On-On-On – Amit 2015-03-25 06:13:12

回答

-1

要在頁面上禁用請求驗證您必須設置頁面指令爲false validateRequest屬性:

<%@ Page validateRequest="false" %> 

要禁用請求驗證您的應用程序,您必須修改或創建一個Web.config文件您的應用程序和節的validateRequest屬性設置爲false:

<configuration> 
    <system.web> 
     <pages validateRequest="false" /> 
    </system.web> 
</configuration> 

如果要禁用請求驗證您的服務器上的所有應用程序,可以讓這個修改您的Machine.config文件。

在.NET 4中,你可能需要添加到web.config中:

<httpRuntime requestValidationMode="2.0" /> 

詳情:

  1. http://www.asp.net/whitepapers/request-validation
  2. http://www.asp.net/whitepapers/aspnet4/breaking-changes#0.1__Toc245724857
+0

是的,我知道這一點,但我不想讓validaterequest爲false。 – 2015-03-25 05:44:44

+1

你最好刪除這個答案,因爲OP不想要這個方法.. – Ravimallya 2015-03-25 05:44:54

相關問題