在ASP.NET MVC中显示JSON格式的错误信息

3

我正在创建我的第一个ASP.NET MVC2项目,使用客户端的jquery和ajax。

现在,当我进行Q.A测试并添加一些测试异常,从我的控制器将其传递给json,类似于这样:

Controller:

public JsonResult PopulateDetails(int id)
        {
                  ProductServiceClient client = new ProductServiceClient();
            try
            {
                if (id == 0)
                { throw new Exception("TEST ERROR."); }

                string name= "test";
                List<Product> product= client.GetPriductbyName(name);
                return Json(product);

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

Jquery-Ajax脚本:

$.ajax({
        type: "POST",
        url: url_ ,
        data: search,
        success: function(data) {   // data: Passed (Records) from the PopulateDetails controller
            displayDetails(data);   // after routing on Populate Details  converts the data to table
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            alert("error: " + XMLHttpRequest.responseText);
        },
        dataType: 'json'
    });

我注意到它会返回以下默认错误信息:
---------------------------
Windows Internet Explorer
---------------------------
error: <html>

    <head>

        <title>TEST ERROR.</title>

        <style>

         body {font-family:"Verdana";font-weight:normal;font-size: .7em;color:black;} 

         p {font-family:"Verdana";font-weight:normal;color:black;margin-top: -5px}

         b {font-family:"Verdana";font-weight:bold;color:black;margin-top: -5px}

         H1 { font-family:"Verdana";font-weight:normal;font-size:18pt;color:red }

         H2 { font-family:"Verdana";font-weight:normal;font-size:14pt;color:maroon }

         pre {font-family:"Lucida Console";font-size: .9em}

         .marker {font-weight: bold; color: black;text-decoration: none;}

         .version {color: gray;}

         .error {margin-bottom: 10px;}

         .expandable { text-decoration:underline; font-weight:bold; color:navy; cursor:hand; }

        </style>

    </head>



    <body bgcolor="white">



            <span><H1>Server Error in '/' Application.<hr width=100% size=1 color=silver></H1>



            <h2> <i>TEST ERROR.</i> </h2></span>



            <font face="Arial, Helvetica, Geneva, SunSans-Regular, sans-serif ">



            <b> Description: </b>An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.



            <br><br>



            <b> Exception Details: </b>System.Exception: TEST ERROR.<br><br>



            <b>Source Error:</b> <br><br>



            <table width=100% bgcolor="#ffffcc">

               <tr>

                  <td>

                      <code><pre>



Line 64:             catch (Exception ex)

Line 65:             {

<font color=red>Line 66:                 throw ex;

</font>Line 67:             }

Line 68:         }</pre></code>



                  </td>

               </tr>

            </table>



            <br>

---------------------------
OK   
---------------------------

现在如何将异常中的错误信息整齐地显示出来,而不是整个 HTML 错误信息。类似于显示警告消息框 alert("TEST ERROR") 的简单方式。
有更简单的方法吗?
我看到这在 asp.net 上链接起来很好 How Do I Handle jQuery Errors and Display them on the Client?,但我找不到 AjaxResponseViewModel() 的声明位置。
1个回答

3
在你的catch中,你应该记录错误并返回一个json友好的结果,例如:

在您的捕获中,您可能应该记录错误,然后返回一个json友好的结果,例如:

 catch (Exception ex)
 {
      //log.Error(ex); using whatever you use to log
      return Json(ex.Message);
 }

或者
return Json("There has been an error, please contact support or read the log");

你所抛出的异常将会完整地返回错误视图。

请问你能否使用我上面的控制器方法,这样我就可以看到应该把它放在哪里。谢谢。 - BizApps
另一个问题是,我该如何在我的Ajax脚本中添加“on error”?谢谢。 - BizApps
假设你有一个 <div id="validationMessage" />,那么在替换 alert 的地方,你可以使用 $('validationMessage').html(XMLHttpRequest.responseText)。 - dove
谢谢...你知道一些使用WCF的MVC应用程序示例项目吗? - BizApps

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接