weixin_33711647 2014-04-29 09:50 采纳率: 0%
浏览 47

MVC中的SQL错误异常

I am trying to show the exception that is being thrown because of SQL store procedure failure.I am getting the response in ajax but that is to with requst.responseText but I dont want to access the whole responseText.I need only the title of this responseText

My Controller is as Follows:

public ActionResult ReOrderSegment(string DocumentIDs, long DossierIDForReOrder) 
        {
            try
            {
                var TrimmedString = DocumentIDs.TrimEnd('#');

                var DocumentIDsWithInteger = Convert.ToInt64(DocumentIDs.Split('#').FirstOrDefault());

                long SelectedCompany = db.Documents.FirstOrDefault(x => x.ID == DocumentIDsWithInteger).CompanyID;

                var Message = db.PerformAutomatedSorting(SelectedCompany, DossierIDForReOrder, TrimmedString);

                return Json(Message, JsonRequestBehavior.AllowGet);

            }
            catch (SqlException ex)
            {
                Response.StatusCode = 500;
                Response.TrySkipIisCustomErrors = true;
                return Json(new { errorMessage = ex.Message }, JsonRequestBehavior.AllowGet);
            }

            return Json(0);
        }

and JS:

 $.ajax({
                    type: 'POST',
                    url: rootUrl("Dossier/ReOrderSegment"),
                    dataType: "json",
                    data: { DocumentIDs: DocumentIDs, DossierIDForReOrder: DossierIDForReOrder },
                    traditional: true,
                    success: function (rest)
                    {
                        alert(rest);

                    },
                    error: function (request, status, error)
                    {
                        //var all = JSON.parse(request.responseText);
                        alert(request.responseText);
                    }   
                });
            });

here i am attaching the images enter image description here

here i want to show only "Please select only Air Product that are UnInvoiced" in alert.

  • 写回答

2条回答 默认 最新

  • 北城已荒凉 2014-04-29 10:10
    关注

    Here is how I am doing it which you may find helpful.

    In base controller:

     protected virtual JsonResult JsonError(Exception error, string message = null, int statusCode = 500)
            {
                JsonNetResult res = new JsonNetResult();
                res.ContentType = "application/json";
                res.ContentEncoding = System.Text.Encoding.UTF8;
                res.Data = new JsonErrorModel(){ error = error, message = message };
    
                if (error != null)
                    Log.Error(message, error);
                else if (!string.IsNullOrWhiteSpace(message))
                    Log.Error(message);
    
                int newStatus = statusCode;
                if (error != null)
                {
                    if (error is ArgumentException || error is ArgumentNullException || error is AbsenceSoftException)
                        newStatus = 400;
                    var aggErr = error as AbsenceSoftAggregateException;
                    if (aggErr != null)
                    {
                        newStatus = 400;
                        message = message ?? string.Join("; ", aggErr.Messages);
                    }
                }
    
                if (Response != null)
                {
                    Response.StatusCode = newStatus;
                    Response.StatusDescription = message ?? (error == null ? "An error has ocurred" : error.Message);
                }
    
                return res;
            }
    

    in HomeController:basecontroller

     public JsonResult TestCase(Model model)
            {
                try
                {
                }
                catch (Exception ex)
                {
                    return JsonError(ex);
                }
            }
    

    However, I am thinking to re-factor it, and to implement it as a reusable exception handling attribute or possibly managing the error handling code logic in global.asax.cs application_error method.

    评论

报告相同问题?