简体   繁体   中英

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. 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:-

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. But it is being displayed in "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:

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

Better explaining what was already commented by 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.

Your could let the exception throw and deal with it at the AJAX "error" block

Or keep it that way and return a compose object, something like this:

[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:

    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 :

  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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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