What is the accepted way to handling errors and trapping exception in asp.net mvc structure?
The general consensus is to have exceptions bubble up.
So what layer (View or Controller) would you handle the exceptions (catch them/ display user friendly text, etc). My hunch is that it is done in the controller?
EDITED:
I would like to avoid repeating the same error handling code in every controller action..hence I am looking for a concise example of how to implement error handling without repeating the same code.
What is the accepted way to handling errors and trapping exception in asp.net mvc
Share
Handling exceptions in the controller could lead to a lot of repetitive code. A better approach is to handle them in an action filter that extends
HandleErrorAttribute. There you could log exceptions and then redirect to a page that displays a nice message indicating the user that something went wrong.However, there are some cases where you need to handle exceptions in your controller methods, for example, when you can recover from an exception and show the user an appropriate message, for example an exception that was throw by your business layer indicating that the values you provided are not valid. In that case, you should catch the specific exception and show the user the same view along with an appropriate message.
EDIT:
}
EDIT 2:
Then you use the attribute like this: