I am implementing exception handling in an ASP.NET MVC 5 application, and have chosen to use the HandleErrorAttribute, here is the code I implement
[HandleError(ExceptionType = typeof(DbUpdateException), View = "Error")]
public class CompaniaController : Controller
{
private CompaniaRepository companiaRepository = null;
public CompaniaController()
{
this.companiaRepository = new CompaniaRepository(new InventarioContext());
}
public ActionResult Listar()
{
var companias = companiaRepository.Listar().ToList();
List<Moneda> monedas = monedaRepository.Listar().ToList();
ViewBag.Monedas = new SelectList(monedas, "MonedaID", "Descripcion");
return View( "Listar", companias);
}
}
I also add the configuration of theweb.config
<customErrors mode="On"></customErrors>
Running the application in debug mode I see that it crashes first, and after pressing F5 the view just comes out with the error message. What is the reason for that? Or what part do I need to configure so that the error view is the one that appears and the application does not crash?
I appreciate your time.
>>when I mention "first it crashes" I mean that the application stops in one of the lines of the controller (where the error occurs), after giving it F5 I just get the view that is configured for the error
But that effect is present because you are debugging with VS, it first stops the code so you can inspect the problem and analyze the detail.
When you confirm that you want to continue (by pressing F5), it continues with the next operation, that is, it executes the action of the attribute to redirect the view of the error.
When you publish the application in the IIS this effect will not take place, but it will be continuous and you will only see the view of the message.
If you don't want the execution to stop in the event of an error, you could uncheck the control of
Exceptions