简体   繁体   中英

Returning custom error from mvc controller to jquery ajax call

What I am trying to do is to pass a custom error from an asp.net mvc4 controller to a jquery.ajax() call. So I have written a custom error filter:

public class FormatExceptionAttribute : HandleErrorAttribute
{
    public override void OnException(ExceptionContext filterContext)
    {
        if (filterContext.RequestContext.HttpContext.Request.IsAjaxRequest())
        {
            filterContext.Result = new JsonResult()
            {
                ContentType = "application/json",
                Data = new
                {
                    name = filterContext.Exception.GetType().Name,
                    message = filterContext.Exception.Message,
                    callstack = filterContext.Exception.StackTrace
                },
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            };

            filterContext.ExceptionHandled = true;
            filterContext.HttpContext.Response.StatusCode = 500;
            filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;
        }
        else
        {
            base.OnException(filterContext);
        }
    }
 }

And I have register it as a global filter by performing:

GlobalFilters.Filters.Add(new FormatExceptionAttribute());

Below my ajax call defined at my mvc4 view (Note that task is a string like "/MyController/MyAction/"):

 function loadData(task) {
     ajax({
         url: task,
         type: 'POST',
         dataType: 'json',
         contentType: "application/json; charset=utf-8"
     }).then(function (data) {
         $(data).map(function (i, item) {
             addNewElement(item);
         })
     },
             function (xhr) {
                 try {
                     // a try/catch is recommended as the error handler
                     // could occur in many events and there might not be
                     // a JSON response from the server
                     var json = $.parseJSON(xhr.responseText);
                     alert(json.errorMessage);
                 } catch (e) {
                     alert('something bad happened');
                 }
             });
 };

So MyAction in Mycontroller looks like the following:

    [HttpPost]
    public ActionResult MyAction()
    {
        try
        {
            var dataCollection = (dynamic)null;

            using (ConfigContext context = new ConfigContext())
            {
                dataCollection = context.MyItems.Where(i=> i.TypeId == 1).AsEnumerable().OrderBy(k => k.Name).Select(w => new
                    {
                        Alias = string.Format("{0}-{1}", Resources.Constants.Prefix, w.Id),
                        Name = w.Name,
                        Desc = w.Desc
                    }).ToArray();
            }

            return Json(dataCollection);
        }
        catch (Exception ex)
        {

            // I want to return ex.Message to the jquery.ajax() call
            JsonResult jsonOutput = Json(
             new
             {
                 reply = new
                 {
                     status = "Failed in MyAction.",
                     message = "Error: " + ex.Message
                 }
             });

            return jsonOutput;
        }
    }

For some reason, in the jquery.ajax() call I am not getting the ex.message error sent by the controller (server side) and in jquery.ajax() when trying to convert to json using:

var json = $.parseJSON(xhr.responseText);

an exception is thrown saying it is not a json result so in the jquery.ajax is entering in the catch body:

 } catch (e) {
     alert('something bad happened');
 }

So What I would like to do is:

  1. Returning the ex.message from the controller to the jquery.ajax() call.
  2. Additionally (nice to have), instead of registering the custom error filter above indicated as a global filter in global.asax.cs, I would like to only apply it to those particular controller/actions that are invoked by ajax calls.
  3. Also (maybe it would be better to open another thread), string concatenation (String.format) in MyAction in the controller throws an exception when I deploy/publish my web app as an application under the default web site on IIS but however it is working ok (not throwing any error) when deploying it as a separate web site. I am using an SQL Server compact edition SQLCe embedded. As far as i know it is not supporting concatenation but i solved this by applying AsEnumerable(). It works when deploying web app as a separate web site but it does not work when deploying it as an application under default web site. Any ideas here?
[HttpPost]
public ActionResult UpdateUser(UserInformation model){
    if (!UserIsAuthorized())
        return new HttpStatusCodeResult(401, "Custom Error Message 1"); // Unauthorized
    if (!model.IsValid)
        return new HttpStatusCodeResult(400, "Custom Error Message 2"); // Bad Request
    // etc.
}


 $.ajax({
                type: "POST",
                url: "/mymvccontroller/UpdateUser",
                data: $('#myform').serialize(),
                error: function (xhr, status, error) {
                    console.log(error); //should be you custom error message
                },
                success: function (data) {

                }
            });

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