How to handle exceptions in ASP.NET MVC 5?

1

I am implementing the exception handling in an ASP.NET MVC 5 application, and I have chosen to use HandleErrorAttribute, here the code that implements

[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 web.config

<customErrors mode="On"></customErrors> 

Executing the application in debug mode, I see that it first falls down, and after giving it F5 the view with the error message appears. To what do I owe that ?, or what part do I need to configure so that the error view is the one that appears and the application does not fall?

I appreciate your time.

    
asked by Michael V. 06.02.2016 в 03:48
source

1 answer

2

> > > > when I mention "first fall" I mean that the application stops on one of the lines of the controller (where the error occurs), after giving F5 I just get the view that this configured for error

But that effect occurs because you are debugging with the VS, this first stops the code so you can inspect the problem and analyze the detail.

When you confirm that you want to continue (pressing F5), it proceeds with the next operation, that is, 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 occur, but it will be continuous and you will only see the message view.

If you do not want the execution to stop before an error you could uncheck the control of Exceptions

    
answered by 06.02.2016 / 23:30
source