0

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: enter image description here

I am using MVC5 with C#

Plexis Plexis
  • 302
  • 2
  • 12
  • if exception is thrown in view - this view is dead and can't be returned – vasily.sib Jun 22 '18 at 06:30
  • 3
    "could not collect the puzzle pieces together!" is not a very concrete question. And asking for a repeat of that entire project/blog is too broad. – bommelding Jun 22 '18 at 06:30
  • @bommelding I meant that I read the different ways, but couldn't know which t use – Plexis Plexis Jun 22 '18 at 06:36
  • 1
    Well, neither can I. – bommelding Jun 22 '18 at 06:39
  • Have you read [ask]? Have you read [mcve]? – Enigmativity Jun 22 '18 at 06:41
  • @Enigmativity yes, and I dont have code to give, since I am unable to find how to write such code, and this is the problem mentioned in the question. – Plexis Plexis Jun 22 '18 at 06:44
  • @PlexisPlexis - But you said "I tried some code found over the internet". You do seem to have code. Also the [ask] page tells you how to ask. Does your question fit the same quality standards mentioned there? – Enigmativity Jun 22 '18 at 06:49
  • @Enigmativity check this https://stackoverflow.com/questions/9/how-do-i-calculate-someones-age-in-c?rq=1 is this a quality question with verifiable code? it has a score of 1741 upvotes and 391 bookmarks! – Plexis Plexis Jun 22 '18 at 06:52
  • @bommelding I didn't mention the blog for you to read it all! I mentioned it as the "Standards" ask us to show what we have done, or to provide our "Search efforts"! – Plexis Plexis Jun 22 '18 at 06:55
  • @PlexisPlexis - Really? Are you saying that your question is as clearly written as that one? – Enigmativity Jun 22 '18 at 07:21
  • @Enigmativity No, may be it is not clear, my point is that lot of questions do not obey the standards and the guidelines u sent, and receive tons of upvotings! – Plexis Plexis Jun 22 '18 at 07:27
  • @PlexisPlexis - Sure, and lots of people break laws so maybe those laws should be scrapped? I showed you the pages published by this site to help improve the quality of the questions. That's all. – Enigmativity Jun 22 '18 at 07:42
  • @Enigmativity yes, I know, and thanks for that, but sometimes, it is bad to feel unsupported! Thank you – Plexis Plexis Jun 22 '18 at 08:11

2 Answers2

1

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...

Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
1

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.

Plexis Plexis
  • 302
  • 2
  • 12