How to return exception data in json format from App_Error in global.asax?
我正在将Asp.net MVC项目作为单页面应用程序进行错误处理。
我希望将
我不想刷新页面或将其重定向到错误页面。
这是要走的路
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | protected void Application_Error(object sender, EventArgs e) { Exception exception = Server.GetLastError(); // if the error is NOT http error, then stop handling it. if (!(exception is HttpException httpException)) return; if (new HttpRequestWrapper(Request).IsAjaxRequest()) { Response.Clear(); Response.TrySkipIisCustomErrors = true; Server.ClearError(); Response.ContentType ="application/json"; Response.StatusCode = 400; JsonResult json = new JsonResult { Data = exception.Message }; json.ExecuteResult(new ControllerContext(Request.RequestContext, new BaseController())); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | //Write This code into your global.asax file protected void Application_Error(Object sender, EventArgs e) { var ex = Server.GetLastError(); //We check if we have an AJAX request and return JSON in this case if (IsAjaxRequest()) { Response.Write(JsonConvert.SerializeObject(new { error = true, message ="Exception:" + ex.Message }) ); } } private bool IsAjaxRequest() { //The easy way bool isAjaxRequest = (Request["X-Requested-With"] =="XMLHttpRequest") || ((Request.Headers != null) && (Request.Headers["X-Requested-With"] =="XMLHttpRequest")); //If we are not sure that we have an AJAX request or that we have to return JSON //we fall back to Reflection if (!isAjaxRequest) { try { //The controller and action string controllerName = Request.RequestContext. RouteData.Values["controller"].ToString(); string actionName = Request.RequestContext. RouteData.Values["action"].ToString(); //We create a controller instance DefaultControllerFactory controllerFactory = new DefaultControllerFactory(); Controller controller = controllerFactory.CreateController( Request.RequestContext, controllerName) as Controller; //We get the controller actions ReflectedControllerDescriptor controllerDescriptor = new ReflectedControllerDescriptor(controller.GetType()); ActionDescriptor[] controllerActions = controllerDescriptor.GetCanonicalActions(); //We search for our action foreach (ReflectedActionDescriptor actionDescriptor in controllerActions) { if (actionDescriptor.ActionName.ToUpper().Equals(actionName.ToUpper())) { //If the action returns JsonResult then we have an AJAX request if (actionDescriptor.MethodInfo.ReturnType .Equals(typeof(JsonResult))) return true; } } } catch { } } return isAjaxRequest; } //Write this code in your ajax function in html file <script type="text/javascript"> $.ajax({ url: Url type: 'POST', data: JSON.stringify(json_data), dataType: 'json', cache: false, contentType: 'application/json', success: function (data) { Successfunction(data); }, error: function (xhr, ajaxOptions, thrownError) { var obj = JSON.parse(xhr.responseText); if (obj.error) { show_errorMsg(obj.message); } } }); |