2014-07-02 25 views
2

我有這樣的代碼不能通過符號和井號在MVC

@using (Ajax.BeginForm("LoadFilter", "Contact", new { filterType = (int)FilterContactBy.Name }, new AjaxOptions { OnBegin = "ShowProgrees", OnSuccess = "HideProgress" })) 
{ 
     @Html.TextBoxFor(m => m.ContactFilter.FilterValue, new { placeholder = "Enter a name" })                          
     <input type="submit" class="link submit green" value="Add Filter" />        
} 

由於相當明顯,ShowProgress和​​方法做什麼,但顯示和隱藏裝載機GIF。

該方法在後端是

[HttpPost] 
public ActionResult LoadFilter(ContactListModel filter, int filterType=0, Boolean isAllFilterApply=true) 
{ 
    if (filter != null) 
    { 
     // process here 
    } 
} 

如果我輸入什麼它工作正常,例如

hi 
test 
me & you 
contact #1 
contact #1 and me 


當我出現任何錯誤,從進入&#在一起。 loader gif只是繼續移動,Controller上的代碼從未被擊中。

另外,在Chrome控制檯中,它顯示500 Internal Server Error(Firefox也是如此!)。

我的解決方案
我試圖escapeenocodeURIComponent,但它並沒有奏效。以下是我試過

$.ajaxPrefilter(function(o, org, xhr) { 
    if (org.url.indexOf('/Contact/LoadFilter?filterType=') > -1) { 
    // console.log(org.data[0].value); 
    org.data[0].value = encodeURIComponent(org.data[0].value); 
    // console.log(org.data[0].value);  
    } 
}); 

&# 
%26%23 

當代替encodeURIComponent我用escape輸出(控制檯)的方法,產量爲同一

&# 
%26%23 

但它仍然不打在控制器上的方法。任何人都可以幫助我獲得解決方案,更重要的是,告訴我爲什麼這是發生在第一位?

PS
請不要問我,什麼是用在接觸過濾器輸入&#的,或者刪除這些並告訴客戶這不是一個有效的輸入。我不能那樣做,所以請不要這樣做。

更新
有沒有人甚至完全讀這個?我不是問如何解碼NO。在標記爲重複或標記爲關閉之前,請閱讀完整的問題。

+0

不需要這種態度,它不會讓人們想要幫助你。 – Liam

+0

@Liam 是的,我知道。請接受我的道歉。 – Razort4x

回答

-3

使用

HttpUtility.UrlDecode("url") 
+0

你讀過這個問題了嗎?它甚至沒有在控制器頁面上執行操作。我知道我可以使用'UrlDecode',但是首先它必須正確地執行操作?這就是問題所在。 – Razort4x

3

好,我知道這一點。

Global.asax在這樣的代碼創建Application_BeginRequest ..

protected void Application_BeginRequest(Object sender, EventArgs e) 
     { 
      var httpApp = (HttpApplication)sender; 
      Debug.Write(httpApp); 
     } 

我坐的httpApp斷點,然後在watch窗口,檢查httpApp.Request,發現它在那裏。它說(something I don't remember right now) threw an exception of type 'System.Web.HttpRequestValidationException'

所以,我的錯誤是因爲這一點,所以在方法LoadFilter,我剛添加的屬性ValidateInput(false),它的工作

[HttpPost] 
[ValidateInput(false)] 
public ActionResult LoadFilter(ContactListModel filter, int filterType=0, Boolean isAllFilterApply=true) 
{ 
    // process here 
} 

雖然,我必須以檢查SQL注入因爲我們正在根據收到的輸入建立一個動態查詢。

+1

在這裏解決了類似情況的優秀調試。只是好奇你用什麼技術來檢查SQL注入?!我們還從輸入中構建了一個動態查詢。 – usefulBee