简体   繁体   English

从Controller中的catch块抛出的消息未正确显示

[英]Message thrown from catch block in Controller is not displayed properly

I am trying to pass an exception message thrown from ASP.NET MVC controller to JQuery Ajax function. 我试图将从ASP.NET MVC控制器引发的异常消息传递给JQuery Ajax函数。 But the message is not being displayed properly. 但是该消息未正确显示。 May be it is passed to the success block and so the color of the error message is not correct while displaying. 可能是它被传递到成功块,因此显示时错误消息的颜色不正确。

In Controller:- 在控制器中:

[HttpPost]
public string ABC()
        {
            try
            {
                //some codes here
                return message;
            }
            catch (Exception ex)
            {
                return "An error has occurred.";

            }
        }

In Ajax function:- 在Ajax函数中:-

success: function (data1) {
                var message = data1;
                 HideMasterProcessing();
                ShowNotificationMessage(message, "notifyMessage", "notify-info", false);
            },


     error: function (data2) {
                    var message = data2;
                    HideMasterProcessing();
                    ShowNotificationMessage(message, "notifyMessage","notify-errror", false);
 }

I want to display the exception message in the "notify-error" div. 我想在“ notify-error” div中显示异常消息。 But it is being displayed in "notify-info" div. 但是它显示在“ notify-info” div中。

You're not returning an error status from the controller, so the result is always treated as a success. 您不会从控制器返回错误状态,因此始终将结果视为成功。 Instead of just returning a string, use an ActionResult as a wrapper so you can specify the status code: 使用ActionResult作为包装器,而不是仅返回字符串,因此可以指定状态代码:

return new HttpStatusCodeResult(500, "An error occurred.");

Better explaining what was already commented by codroipo: 更好地解释codroipo已经评论的内容:

When you return an error message within your catch block, the AJAX function consider it a success, which means it never lands on AJAX "error" block. 当您在catch块中返回错误消息时,AJAX函数将其视为成功,这意味着它永远不会落在AJAX“错误”块上。

Your could let the exception throw and deal with it at the AJAX "error" block 您可以让异常抛出并在AJAX“错误”块中对其进行处理

Or keep it that way and return a compose object, something like this: 或者保持这种方式并返回一个compose对象,如下所示:

[HttpPost]
public string ABC()
        {
            try
            {
                //some codes here
                return new {Message = message, Error = null};
            }
            catch (Exception ex)
            {
                return new {Message = null, Error = "An error has occurred."};    
            }
        }

In Ajax function: 在Ajax函数中:

    success: function (data1) {
                    HideMasterProcessing();
                    if(data1.Error == null)
                    {
                        var message = data1.Message;                     
                        ShowNotificationMessage(message, "notifyMessage", "notify-info", false);
                    }
                    else
                    {
                        var message = data1.Error;
                        ShowNotificationMessage(message, "notifyMessage","notify-errror", false);
                    }
                },


         error: function (data2) {
                        var message = data2;
                        HideMasterProcessing();
                        ShowNotificationMessage(message, "notifyMessage","notify-errror", false);
     }

Let me know if it turns out ok! 让我知道是否可以!

This worked for me : 这为我工作:

[HttpPost]

    public string ABC()
            {
                try
                {
                    //some codes here
                    return message;
                }
                catch (Exception ex)
                {

                 var message = "An error has occurred.";
                 return message;

                }
            }

In Ajax : 在Ajax中:

  success: function (data1) {
                if (data1 === "An error has occurred.")
                {
                    HideMasterProcessing();
                    ShowNotificationMessage("An error has occurred. Please contact administrator.", "notifyMessage", "notify-error", false);
                }
                else
                {
                    HideMasterProcessing();
                    var message = data1;
                    ShowNotificationMessage(message, "notifyMessage", "notify-info", false);
                }

I just compared the string that was passed from controller with the data that was entering in the success block and then displayed it in the required div. 我只是比较了从控制器传递的字符串和在成功块中输入的数据,然后将其显示在所需的div中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM