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