Error handling in controllers with MVC
Posted
by twrn
on Stack Overflow
See other posts from Stack Overflow
or by twrn
Published on 2010-05-04T14:46:40Z
Indexed on
2010/05/04
14:48 UTC
Read the original article
Hit count: 187
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");
}
}
© Stack Overflow or respective owner