0

When I try to propagate an exception and pass it as parameter into my ErrorController, it is always null.

Controller

public ActionResult Test()
{
    try
    {
        throw new Exception("ALGO");
        //
        return View();
    }
    catch (Exception ex)
    {
        return RedirectToAction("Error", "Error",
                new
                {
                    exception = ex,
                    controller = this.ControllerContext.RouteData.Values["controller"],
                    action = this.ControllerContext.RouteData.Values["action"]
                });
    }
}

ErrorController

public ActionResult Error(Exception exception, string controller, string action)
{
    // exception is always null...
    Response.StatusCode = 500;
    ViewBag.exception = new HandleErrorInfo(exception, controller, action);

    return View();
}

Any idea how to get the exception properly?

Is there a better approach for error handling?

I also tried this one but I got several errors because of parameteless constructor for handleerrorinfo

blfuentes
  • 2,731
  • 5
  • 44
  • 72

1 Answers1

0

Whenever you use RedirectToAction, it performs an HTTP redirect. Any of the values you pass have to be primitive types, since they will be appended to the redirect URL. That means that you cannot pass an entire object, like you are trying to do with the exception. The easiest thing that you can do is to replace the RedirectToAction with

return Error(ex, this.ControllerContext.RouteData.Values["controller"], this.ControllerContext.RouteData.Values["action"]);

This approach will still call your Error method and display the View properly, but it will not change the URL like a redirect would. If you wanted to use this method, then you could try using javascript to change the URL.

Also, do you really want to display all of the error details to your end user? If you are just using this to display a plain error page without details then you could look into simply using the customErrors attribute in your web config to redirect to an error page. That way all that your end user knows is that some error occured.

dabecks
  • 153
  • 1
  • 1
  • 10
  • My case is for Debugging purposes. I would like to show different "views-Output". I was trying to use customerrors Settings but according to this: http://stackoverflow.com/questions/2480006/what-is-the-difference-between-customerrors-and-httperrors customerrors is being obsolete – blfuentes Jul 01 '15 at 06:39
  • How do I return that "Error" method? Where does it belong? – blfuentes Jul 01 '15 at 06:46
  • @blacai just replace your return RedirectToAction with the return statement in my answer. That should do it. – dabecks Jul 01 '15 at 11:11
  • @blacai If this is strictly for debugging purposes, have you considered using Elmah? https://www.nuget.org/packages/elmah/ – dabecks Jul 01 '15 at 11:15
  • the Error method is not being recognized.... I have already elmah and it works fine. But I still would like to customize my error views showing info about the exception while running on debug. – blfuentes Jul 01 '15 at 11:16
  • @blacai my bad. I forgot it was in a separate controller. Change return Error to return new ErrorController().Error – dabecks Jul 01 '15 at 11:19
  • Thank for the answer, but it is not complete and it is not working at all. If I call the Controller directly like you said my `Response` object in the `Error` is null. Also if I remove it, the view is not showing... If you want to help with a better answer, please test it before. – blfuentes Jul 02 '15 at 07:03