Redirect to error page when entering catch

3

I'm having a question about a possible improvement in the method when an error occurs in a Action and redirects to an error page. I currently do this in Action :

 public ActionResult Index()
    {
        try
        {
            //Um código qualquer aqui 
            return View();
        }
        catch (Exception ex)
        {
            return RedirectToAction("Index", "Erro");
        }
    }

That is, if I give a code error I redirect to Action Index of Controller Erro . It turns out that this method I use has a problem. When the View that I'm going to return is within Modal , the error page is sort of displayed within Modal , leaving the layout totally unconfigured .

With this, I would like to know if there is a better or more correct way than when throwing an error exception, redirect to the correct page.

    
asked by anonymous 11.06.2016 / 15:49

1 answer

0

Hello,

This will depend a lot on the rules of your system, but usually the common one is to let the error occur in the application so that your server returns a 500 code when an error occurs.

Just like a page not found returns a status code 404, when an error happens internally in your application the request returns a status code 500.

These errors already redirect the client to a default page, but you can customize to which pages the client will be redirected in case of an error in the server through the web.config of your application adding these settings:

<customErrors mode="On"  defaultRedirect="~/Error/500">
  <error statusCode="404" redirect="~/Error/404" />
  <error statusCode="500" redirect="~/Error/500" />
</customErrors>

You can add settings for all status codes where redirect is the Action to which the client will be redirected.

The defaultRedirect property is the Action to which the client will be redirected if the error status code is not specified.

Using this approach your application will generate error logs on your server when something happens, this can be very useful if you need to check something out.

    
25.06.2016 / 22:57