【问题标题】:Kendo UI - how to implement Error Handling on WEB API, non MVC using Kendo GridKendo UI - 如何在 WEB API 上实现错误处理,非 MVC 使用 Kendo Grid
【发布时间】:2012-11-15 09:22:09
【问题描述】:

我的场景是我创建了一个返回 Active Directory 对象的 Web API。

我有这个 WEB API 函数创建 Active Directory 用户并创建一个用户,该用户返回包含 First Name , Last Name, Email, UserName, Etc. 的 Active Directory 对象。如果出现错误我将如何处理?

我正在使用 Kendo Grid 内联编辑 http://demos.kendoui.com/web/grid/editing-inline.html
我想将错误消息显示为弹出窗口

我该怎么做???

选项

  1. 尝试捕获错误并将 Active Directory 对象作为 例外???

    • 如何捕捉这是 Kendo UI?
  2. 抛出响应并获取错误消息并在 Kendo Grid 中显示

    //HttpResponseMessage msg = new HttpResponseMessage(HttpStatusCode.OK) //{ // Content = new StringContent(string.Format("No User with ID = {0}.{1}", businessObject.UserName, ex.InnerException.ToString() )), // ReasonPhrase = "数据库中未找到客户 ID!" //}; //抛出新的HttpResponseException(msg);

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

谢谢, 马克莱文

【问题讨论】:

    标签: kendo-ui


    【解决方案1】:

    每当 KendoUI 通过 Ajax 绑定时,它都依赖于在 json 响应中发送的 ModelState 的序列化版本。本质上,如果 ModelState 无效,则返回到小部件(在本例中为网格)的 json 响应将包含以下内容:

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

    如果您希望网格对其做出响应,基本上您的 WebAPI 将需要返回类似的数据结构。

    【讨论】:

      【解决方案2】:

      这是关于您的选项 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/"
                          }
                      }
                  }
              );
      

      【讨论】:

        猜你喜欢
        • 2013-08-25
        • 1970-01-01
        • 2013-03-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多