C # MVC automatic error handling

I have this code.

public ActionResult Index() { ReceiptModel model = new ReceiptModel(); try { model = new ReceiptModel(context); } catch (BussinessException bex) { ModelState.AddModelError("Index", bex.MessageToDisplay); return View("Index"); } return View(model); } 

BussinesException ir is returned from the database and then displayed to the user. I have to impose a try-catch method on each controller, which is a bit tedious. Is there an easier way to handle these exceptions?

PS All other exceptions are handled using HandleExceptionAttribute

UPDATE:

I used the Floradu88 approach. So now I have something like this.

 public sealed class HandleBussinessExceptionAttribute : HandleErrorAttribute, IExceptionFilter { public override void OnException(ExceptionContext filterContext) { filterContext.Controller.TempData["UnhandledException"] = filterContext.Exception; filterContext.ExceptionHandled = true; ((Controller)filterContext.Controller).ModelState.AddModelError( ((BussinessException)filterContext.Exception).Code.ToString(), ((BussinessException)filterContext.Exception).MessageToDisplay ); filterContext.Result = new ViewResult { ViewName = this.View, TempData = filterContext.Controller.TempData, ViewData = filterContext.Controller.ViewData, }; } } 

and in the Controller action I will put

 [HandleBussinessExceptionAttribute(Order = 2, ExceptionType = typeof(BussinessException), View = "Login")] 

I also tried an exception handler:

  filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(filterContext.RouteData)); 

and then handle the error in the action using ModelState.IsValid , but the values ​​to the action will be null. Therefore, at the moment I am using the first approach. When I have a little more time, I will try to fix the second approach.

+4
source share
2 answers

Please see the documentation for this part: http://msdn.microsoft.com/en-us/library/gg416513%28v=vs.98%29.aspx
http://www.asp.net/web-api/overview/web-api-routing-and-actions/exception-handling
http://www.asp.net/mvc/tutorials/older-versions/controllers-and-routing/understanding-action-filters-cs

Too much content to post here:

  public class NotImplExceptionFilterAttribute : ExceptionFilterAttribute { public override void OnException(HttpActionExecutedContext context) { if (context.Exception is NotImplementedException) { context.Response = new HttpResponseMessage(HttpStatusCode.NotImplemented); } } } 

And your controller:

 public class ProductsController : ApiController { [NotImplExceptionFilter] public Contact GetContact(int id) { throw new NotImplementedException("This method is not implemented"); } } 
+2
source
  • According to this post , create your own binder and put the model in TempData:

     public class AppBinder : DefaultModelBinder { protected override void OnModelUpdated(ControllerContext controllerContext, ModelBindingContext bindingContext) { base.OnModelUpdated(controllerContext, bindingContext); controllerContext.Controller.TempData["model"] = bindingContext.Model; } } 
  • Register it in global.asax:

     ModelBinders.Binders.DefaultBinder = new AppBinder(); 
  • Create a BaseController and override OnException:

     protected override void OnException(ExceptionContext filterContext) { filterContext.ExceptionHandled = true; this.ModelState.AddModelError(string.Empty, filterContext.Exception.Message); filterContext.Result = new ViewResult { ViewName = filterContext.RouteData.Values["action"].ToString(), TempData = filterContext.Controller.TempData, ViewData = filterContext.Controller.ViewData }; base.OnException(filterContext); } 

All actions int Controllers that are inherited from this base controller will show their unhandled exceptions in their own view during the check process (remember that

 @Html.ValidationSummary(true) 

On the page). it works for me, hopefully for you too!

0
source

All Articles