tags:

views:

49

answers:

1

Does it make sense to do error handling and logging inside actions methods or handle the OnException method inside the controllers. One way means writing try/catches in all the action methods even when there is nothing to be done to recover from the error. Handling this at the controller level would allow logging and redirection to an error handler page without writing try/catches inside all the action methods.

Which method makes the most sense? Here is example code of try/catches in an action method.

        [HttpPost]
        public ActionResult Delete(int id)
        {
            using (new Tracer("Project Controller"))
            {
                try
                {
                    Logger.Write("Deleting project");
                    projService.DeleteProject(id);
                    TempData["message"] = "Project Deleted successfully";
                }
                catch (System.Exception ex)
                {
                    HandleException(ex, "Project could not be deleted.");
                }
                return RedirectToAction("List");
            }
        }
A: 

There is already a start in the form of the HandleError attribute.

I suggest you either subclass it or look at its implementation to come up with your custom one. It's really simple.

HandleErrorAttribute.cs

Developer Art