I have finished developing a suitable solution.
For those who want a similar answer to the question I asked, I made a special filter announcement. The main part of this is that the result of the filter is returned as JSON, but even then it will return with success in the ajax jQuery call because it returns a status of 200, which jquery ajax reads as success.
Jquery ajax reads any status outside 200 as an error, so you can see that I changed the status code to the own number that I created, and I will document it, and so jquery ajax sees the error and gives it an ajax error.
public class MyErrorHandlerAttribute : FilterAttribute, IExceptionFilter { public void OnException(ExceptionContext filterContext) { filterContext.HttpContext.Response.StatusCode = 11001; filterContext.ExceptionHandled = true; filterContext.Result = new JsonResult { Data = new { success = false, error = filterContext.Exception.ToString() }, JsonRequestBehavior = JsonRequestBehavior.AllowGet }; } }
To reference the current filter, you simply add an error handler attribute to the function, as shown in the first line below:
[MyErrorHandler] public JsonResult create(MyModel.New details) { try { Library.insert(details); return Json(true); } catch (Exception ex) { return Json(ex.Message); } }
Cyassin
source share