Error handling in C#!

Asked

Viewed 316 times

0

Guys, the following is, in my application I would like to see a page with the error occurred, but that 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. Below the code I’m using:

No 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 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 ta so:

<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 blank and doesn’t even bring the menu together?

With the changes suggested by the friend below this error appears: inserir a descrição da imagem aqui

1 answer

1


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

  1. Check if this view is under the same name as your action?

    • If you’re not, you need to be because if you’re not coming back.
  2. 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 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 it this way 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);

}
  1. The page has no layout declaration nor model declaration, so it returns blank.

    Add the following code at the beginning of your view, don’t forget to put the namespace before the class on the model:

    @{
        Layout = "~/Views/Shared/_Layout.cshtml";    
    }
    @model ResponseViewModel
    
  • Failed, error appeared "Runtime Error Description: An Exception occurred while Processing your request. Additionally, Another Exception occurred while executing the custom error page for the first Exception. The request has been terminated. "

  • Which Exception? Could you put an image of it, or add your question to the details of Exception?

  • I edited the question putting the image of error, see there.... face this is breaking my head.... I know what to do, even more than I’m starting now with C#, I came from PHP!

  • Go debug the code, and when you give the Exception, put the stacktrace on it, please.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.