2012-12-04 50 views
1

請看下面的代碼。它在handler.asxh中。值不能爲空。參數名稱:字符串

public void ProcessRequest(HttpContext context) 
    { 
     context.Response.ContentType = "application/json"; 
     new RequestManagementFacade().PinRequest(Int32.Parse(context.Request.QueryString["requestId"]), (Boolean.Parse(context.Request.QueryString["isPinned"]))); 
    } 

這是示出了下面的錯誤。

值不能爲空。參數名:字符串

有,因爲我有checke D出現上下文請求的查詢字符串傳遞價值,但在這個階段的代碼中斷。

該處理程序將連接到業務邏輯層。

有人能請指教嗎? 在此先感謝

都庫什

+0

可能重複當我錯過了[什麼是.NET一個NullReferenceException?](http://stackoverflow.com/questions/4660142/what-is-a- nullreferenceexception-in-net) –

+0

當然,requestId或isPinned爲null,評估它們並捕獲一些錯誤! –

回答

4

有,因爲我有checke D出現上下文請求的查詢字符串

我強烈懷疑你的診斷是不正確的,然後傳遞價值。價值觀不會神奇地消失 - 你需要質疑你的假設。雖然這很容易調試。我建議改變你的代碼:

public void ProcessRequest(HttpContext context) 
{ 
    context.Response.ContentType = "application/json"; 
    string requestId = context.Request.QueryString["requestId"]; 
    string isPinned = context.Request.QueryString["isPinned"]; 
    var facade = new RequestManagementFacade(); 
    facade.PinRequest(Int32.Parse(requestId), Boolean.Parse(isPinned)); 
} 

它當時真的簡單步,找出發生了什麼事情。

+0

如果將解析代碼放在單獨的行上,它更簡單:int ID = Int32.Parse(requestId);爲了更強健,您可以使用TryParse並自動處理錯誤。 –

+0

@SteveWellens:好的,如果問題是'requestId'或'isPinned'是空的,那麼在解析調用之前應該清除它。 –

+0

感謝您的幫助 – Kush

2

很可能要麼context.Request.QueryString["requestId"]context.Request.QueryString["isPinned"]沒有返回一個有效的字符串值。檢查兩個值是否在查詢字符串中傳遞了正確的ID,當然是requestIdisPinned

+0

值傳遞給我插入它作爲 的處理程序時好解決「PinRequest.ashx?=」 +的requestId + isPinned」 這給了我結果2True 所以,我實現了打嗝是用不包括字符串名稱 「PinRequest.ashx?的requestId =」 + this._requestId + 「&isPinned =」 + this._isPinned 感謝您幫助球員 LeviBotelho謝謝你讓我查的東西作爲它的JavaScript檢查時,我被錯過了。 – Kush

1

傳遞值時,我插入它作爲

"PinRequest.ashx?="+requestId+isPinned" 

這給了我結果2True

處理程序所以,我實現了打嗝是用不包括字符串名稱

"PinRequest.ashx?requestId=" + this._requestId + "&isPinned=" + this._isPinned 
好解決

感謝您的幫助球員

LeviBotelho Tha NK你讓我查的東西作爲它的JavaScript檢查

相關問題