2012-11-15 63 views
2

我的情況是我創建了一個Web API返回一個Active Directory對象。劍道UI - 如何實現錯誤處理的WEB API,非MVC使用劍道電網

我有這個WEB API函數創建Active Directory用戶並創建一個返回包含First Name , Last Name, Email, UserName, Etc.的Active Directory對象的用戶。如果發生錯誤,我該如何處理?

我使用的劍道電網聯編輯http://demos.kendoui.com/web/grid/editing-inline.html
我想顯示錯誤消息作爲彈出窗口

我該怎麼做???

選項

  1. 嘗試捕獲錯誤並放置在Active Directory對象爲 異常???

    • 我該如何捕獲這是Kendo UI?
  2. 投擲的響應和收到錯誤消息,並在劍道網格

    // HttpResponseMessage味精=新HttpResponseMessage(HttpStatusCode.OK) // {// 含量=新的StringContent顯示它( string.Format(「沒有用戶ID = {0}。{1}」,businessObject.UserName,ex.InnerException.ToString())), // ReasonPhrase =「CustomerID Not Found in Database!」 //}; //拋出new HttpResponseException(msg);

OR

//var message = string.Format("Error Message: {0}", taskCreateADUser.ADExceptionDescription); 
       //throw new HttpResponseException(
       // Request.CreateErrorResponse(HttpStatusCode.OK, message)); 

感謝, MarcLevin

回答

3

每當KendoUI通過Ajax結合它依賴於JSON響應發送ModelState中的序列化版本。從本質上講,如果ModelState中是無效的,JSON響應返回給控件(格在這種情況下)將包含這樣的事情:

{ 
    "Errors":{ 
    "PropA":{ 
     "errors":[ 
      "Error1", 
    "Error2" 
     ] 
    }, 
    "PropB":{ 
     "errors":[ 
      "FUBAR" 
     ] 
    } 
    } 
} 

本質上你的WebAPI需要返回類似的數據結構,如果你想電網應對到它。

0

這是關於您的選項2.您將需要應用正確下面您的具體方案。這只是一個非常簡單的響應分析的示例,並在檢測到錯誤時顯示警報。這個示例需要包含一個Items數組的JSON對象。一旦你有了基本想法,你一定可以應用更高級的處理。

$("#grid").kendoGrid({ 
       dataSource: { 
        schema: { 
         data: function(data) { 
          if (data.Items[0].substring(0,37) == "allmyerrormessagesstartwiththisphrase"){ 
          alert(data.Items[0]; 
          } else { 
           return data.Items; 
          } 
         } 
        }, 
        transport: { 
         read: "http://myurl.com/something/" 
        } 
       } 
      } 
     );