2

I´m trying to redirect to an action from one controller to another if something in a try-block goes wrong. What I want to achieve is a general way of presenting a view to the user if something goes wrong in different controllers by directing all errors to an errorhandling ActionResult in my Homecontroller. This is basically what the code looks like:

    try
    {
        Code that may go wrong
    }
    catch (Exception e)
    {
        set the errorcode (integer)

        Logg the error (write a simple textfile)

        RedirectToAction("ErrorHandling", "Home", errorcode);
    }

And in the Homecontroller i would like to generate a view, telling the user that something went wrong:

    public ActionResult ErrorHandling(int errorcode)
    {
        do something with the errorcode

        return View(different view depending on errorcode);
    }

My problem is that if i manipulate the code so that an exception is thrown every step in the catcblock is executed except for the RedirectToAction whic is being ignored. What am i missing? I´m kind of new to this, so hopefully there is a simple answer that i haven´t been able to find...

tereško
  • 58,060
  • 25
  • 98
  • 150
Ebbe
  • 43
  • 6

1 Answers1

2

In your catch block try

  return new RedirectToRouteResult(new RouteValueDictionary
    { 
        {"Controller", "Home"}, 
        {"Action", "ErrorHandling"},
        {"errorcode", errorcode}
    });

Maybe you simply forgot the return in your code:

return RedirectToAction("ErrorHandling", "Home", errorcode);
Hauzi
  • 133
  • 7
  • I tried your suggestion but I still can't get it to work. The RedirectToRouteResult is ignored... As well as the RedirectToAction I've tried them both separateyl but neither of them works. Maybe I should add that my try/catch is within a public ActionResult? I don't know if that might effect something? – Ebbe Nov 28 '11 at 15:07
  • I changed the RedirectToRouteResult to: `return new RedirectToRouteResult(new RouteValueDictionary { {"Controller", "Home"}, {"Action", "ErrorHandling"} });` and now it works for some reason. And i pass the parameters by using TempData when passing from one controller to another and ViewData when passing from a controller to the view. Is that the right/smart way to do it or is there a better way? – Ebbe Nov 29 '11 at 09:09
  • Again: I'm shure the magic lies in the word **return**. Check in your previous attempt if you accidently omitted it. I think the way you are doing it now is right/smart. I'm just not shure if I understand this TempData thing completely ... – Hauzi Nov 29 '11 at 18:23
  • TempData is from what I understand a collection which is persisted between the action redirects for a single request. – Ebbe Nov 29 '11 at 20:58
  • By the way, i used the `return` keyword in my previous attempts, so i can't figure what was wrong. But since the code only does what i tell it to do, i´ve must have told it something wrong i guess... ;-) – Ebbe Nov 29 '11 at 21:01
  • arrg. Common mistake, forgetting the return statement :-) – netfed Apr 15 '20 at 20:13