I need a way to handle exceptions globally, in a way that a message(for example popup message) is shown in the same view that the error was thrown in ( That is without redirecting to an separate error view)
something like:
I am using MVC5 with C#
I need a way to handle exceptions globally, in a way that a message(for example popup message) is shown in the same view that the error was thrown in ( That is without redirecting to an separate error view)
something like:
I am using MVC5 with C#
There is no elegant way to have a global exception handler and keep on the same page.
The global.asax is the place where you do global error handling in Asp.Net, if you want to remain on the page just use try/catch in the Controller.
Tip: Sometimes when an exception occurs, it's too late at the global exception handler as the stack has unwound and you can't even see which page/function/parameters caused the error. Unless you get a memory dump. This behaviour applies to Winform, WPF, Asp.Net...
I solved it using a custom approach. What I did is that I added try-catch for the methods I need to handle.
And in the catch block I set a ViewBag.ErrorMessage
to the message exception, and then return the view of the action method (for example, Create, Edit) and in the view I retrieve the ViewBag.ErrorMessage
and show it.