Error handling in C #!

0

Galera, it's the following, in my application I would like a page to appear with the error occurred, but this page has all the formatting proposed in the layout, so this is not happening because it only generates a blank page with the error code. Here are the code I'm using:

In the homeController:

public ActionResult ExemploErroNaoMapeado()
        {
            var response = new ResponseViewModel();
            try
            {
                throw new Exception("Oops, ocorreu um erro");
            }
            catch (Exception e)
            {
                return ErroCapturado(e);
            }
            return Json(response, JsonRequestBehavior.AllowGet);
        }

        public ActionResult ErroCapturado(Exception ex)
        {
            var response = new ResponseViewModel
            {
                Data = ex.Data,
                Sucesso = false,
                Mensagem = ex.Message
            };

            return Json(response, JsonRequestBehavior.AllowGet);

        }

In manageViewModels I've added:

 public class ResponseViewModel
    {
        public object Data { get; set; }
        public bool Sucesso { get; set; }
        public string Mensagem { get; set; }
    }

And the page that should display the error looks like this:

<h2>
    Ocorreu um erro não mapeado durante a execução
    da última ação...
</h2>


<script type="text/javascript">

    $(document).ready(function () {
        //debugger;
        gerandoRelatorio();
        function gerandoRelatorio() {
            $.getJSON("Home/ExemploErroNaoMapeado", function (response) {

                if (response.sucesso) {
                    console.log(response.data);
                }
                else {
                    alert(data.mensagem);
                }

            }).fail(function (response) {
                //Erro genérico
                alert("Não foi possível processar a sua requisição");

            });
        }
    });
</script>

So I want to know how to make the layout work on this page, since it loads all white and does not even bring the menu together?

With the changes suggested by the friend below, this error appears:

    
asked by anonymous 29.01.2018 / 11:12

1 answer

2

I'll put the steps here, and a change in your controller:

  • Checked if this view has the same name as your action?

    • If it is not, then it must be, otherwise it will not return.
  • To return the view, you have to return not a JsonResult but a view with the error model.

    • What happens is that a JsonResult is most commonly used when you make ajax calls to the controller. If it is a common call, switch to another ActionResult, or make a RedirectToAction, which returns an error view;
  • Do the following to see if it works:

    public ActionResult ExemploErroNaoMapeado()
    {
        var response = new ResponseViewModel();
        try
        {
            throw new Exception("Oops, ocorreu um erro");
        }
        catch (Exception e)
        {
            return RedirectToAction("ErroCapturado", e);
        }
        return Json(response, JsonRequestBehavior.AllowGet);
    }
    
    public ActionResult ErroCapturado(Exception ex)
    {
        var response = new ResponseViewModel
        {
            Data = ex.Data,
            Sucesso = false,
            Mensagem = ex.Message
        };
    
        return View(response);
    
    }
    
  • The page has no layout or model declaration, so it returns blank.

    Add the following code at the beginning of your view, remembering to put the namespace before the class in the model:

    @{
        Layout = "~/Views/Shared/_Layout.cshtml";    
    }
    @model ResponseViewModel
    
  • 29.01.2018 / 11:38