Everything,
I study MVC and use it for a business application (MVC 1.0).
I am really trying to understand exception handling. I spent a lot of time on the Internet, but did not find anything similar to what I need.
We are currently using the filter attribute, which implements IExceptionFilter. We decorate the base controller class so that all exceptions on the server side are well redirected to the exception page, which displays an error and logs.
I started using AJAX calls that return JSON data, but when the server-side implementation causes an error, the filter starts, but the page does not redirect to the Error page - it just stays on the page called the AJAX method.
Is there a way to force redirect to server (e.g. ASP.NET Server.Transfer or redirect?)
I read that I have to return a JSON object (.NET Exception wrapping) and then redirect to the client, but then I can not guarantee that the client will redirect ... but then (although I probably have something wrong) the server tries to redirect, but then receives an unauthorized exception (the base controller is protected, but the Exception controller is not the same as it does not inherit)
Someone please get a simple example (.NET and jQuery code). I feel like I accidentally try in the hope that this will work.
Exception filter so far ...
public class HandleExceptionAttribute : FilterAttribute, IExceptionFilter { #region IExceptionFilter Members public void OnException(ExceptionContext filterContext) { if (filterContext.ExceptionHandled) { return; } filterContext.Controller.TempData[CommonLookup.ExceptionObject] = filterContext.Exception; if (filterContext.HttpContext.Request.IsAjaxRequest()) { filterContext.Result = AjaxException(filterContext.Exception.Message, filterContext); } else { //Redirect to global handler filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new { controller = AvailableControllers.Exception, action = AvailableActions.HandleException })); filterContext.ExceptionHandled = true; filterContext.HttpContext.Response.Clear(); } } #endregion private JsonResult AjaxException(string message, ExceptionContext filterContext) { if (string.IsNullOrEmpty(message)) { message = "Server error"; //TODO: Replace with better message } filterContext.HttpContext.Response.StatusCode = (int)HttpStatusCode.InternalServerError; filterContext.HttpContext.Response.TrySkipIisCustomErrors = true; //Needed for IIS7.0 return new JsonResult { Data = new { ErrorMessage = message }, ContentEncoding = Encoding.UTF8, }; } }
ajax exception-handling asp.net-mvc
Grahamb
source share