Correct error handling in ASP.NET MVC2

I have an OverException (ExceptionContext filterContext) override in my base controller to catch the application during any errors and then register them. The problem I get in my application is that this particular method is run four times for certain errors. I will guide you through the scenario:

Let's say I go to: http: // localhost: 180 / someController / someAction? SomeId = XX

And I have mistreatment of objects in my code. The identifier passed through it is invalid and it retrieves some null object, then I, bc of processing my bad object, try to work with the null object. I get an exception.

Enabling BaseController OnException here.

This null object still returns to the view, where the view is trying to associate it with something or what you have.

BaseController OnException is run again here, for a view error.

In fact, for me, only one error is important, but the leakage effect leads to the fact that more errors are triggered, and spam in my mailbox: - /.

What is the correct way to catch an error in MVC2 and not have it with me?

+6
c # error-handling asp.net-mvc-2
source share
2 answers

I would recommend you inherit the HandleError attribute and move the exception handling. Overriding OnException on one controller means that you have a lot of exception handling code in many controllers or inherit from the base one, which is not really required due to the MVC pipeline.

Using the attribute, you must have one error occurrence for each action, and as soon as the error is processed, it will not start again. Hope this reduces recurring exception messages.

I personally use attributes to handle exceptions, because they are cleaner and more reusable, and get rid of a lot of noise in my actions.

+1
source share

First explain why you are getting a few errors. The first mistake will be to try to work with the null object most likely in your model or controller. Then you will probably get the second exception when the view tries to bind to the null object, when it expects the object to exist. It's not entirely clear why you get 4 errors, but maybe because the code is trying to work with an object that is currently null.

My first suggestion would be for your OnException code to redirect the application to a friendly error page. You probably just eat every new exception and prevent web.config from handling error pages correctly if you have this setting to display the error page.

My second suggestion would be to add code to check for null objects before you start using them. These are commonly called โ€œProtection Clausesโ€ and are very useful and useful for implementation. Then you can determine a convenient way to handle errors without registering an exception if you do not need to also display a friendly message to the user, except for the general "An error has occurred." message.

For example, in your controller, you can check for a null object and pass an alternate view to the user if that object is null

Function Example As ActionResult dim obj as Object = GetObject If obj is Nothing Then Return View("FriendlyNoObjectView") Else Return View(obj) End If End Function 

I know this is vb (Sorry, I know it is better than C #), but the idea is the same. If you want, you can still register this as an error, but then you would not allow the error to be repeated many times. It is always good to cope with an error when it occurs and try to prevent it from popping up to the stack and cause several other errors.

Hope this helps me just understand my thoughts on reading your question.

0
source share

All Articles