3

クライアント側でjqueryとajaxを使用して最初のASP.NETMVC2プロジェクトを作成しています。

ここで、アプリケーションのQAテストを行っていたときに、テスト例外を追加して、コントローラーからjsonに次のように渡します。

コントローラ:

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でリンクされているのを見ましたが、jQueryエラーを処理してクライアントに表示するにはどうすればよいですか?

しかし、AjaxResponseViewModel()がどこで宣言されたのかわかりません。

4

1 に答える 1

3

キャッチでは、おそらくエラーをログに記録してから、 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");

上記では、エラービューを完全に返す例外をスローしています。

于 2012-11-16T09:08:09.213 に答える