Asp.net rewrite OnException to return json or jump to a new page, onexceptionjson
Protected override void OnException (ExceptionContext filterContext) {// exception records can be recorded in the database or text, or other logging components. // Obtain this Exception through filterContext. Exception. FilterContext. exceptionHandled = true; // organize the web. config configure customerror to process string requestType = filterContext. httpContext. request. requestType. toString (); // obtain the request type UrlHelper url = new UrlHelper (filterContext. requestContext); // determines whether the request is a get request. if the request is a get request, the system redirects to the specified page. if the request is not a json if (requestType. toUpper () = "GET") {filterContext. result = new RedirectResult (url. action ("error", "Error"); // jump to the new page} else {filterContext. result = new JsonResult () {Data = new {errorcode = 2, message = filterContext. exception. message}, JsonRequestBehavior = JsonRequestBehavior. allowGet}; // return json data} // execute OnException // base in the base class. onException (filterContext );}