4

How can I redirect to another page from Application_Error?

At present i am doing

Response.Redirect("~/Account/LogOn");

but i would like to do some thing like RedirectToAction()

Pedro Franco
  • 1,926
  • 1
  • 15
  • 37
Kuttan Sujith
  • 7,889
  • 18
  • 64
  • 95

4 Answers4

5

Application_Error is not really a designed way to handle errors in MVC application.

The prefered ways are:

Some more links that can be helpful:

Also, I would recommend using ELMAH if you're not using it right now. You can get it as NuGet package.

Community
  • 1
  • 1
Jakub Konecki
  • 45,581
  • 7
  • 87
  • 126
4
HttpContext.Current.Response.RedirectToRoute(...)
Alexandre
  • 13,030
  • 35
  • 114
  • 173
  • 1
    The context is not available before the application startup is completed. In most cases of handling global errors, this will throw an exception at runtime. Not sure why this has been voted up so much, it's not a good answer. – Doug Beard Feb 24 '17 at 19:42
1
var urlHelper = new UrlHelper(HttpContext.Current.Request.RequestContext).Action("ServerError", "Error");

Response.Redirect(urlHelper, true);
Chuck D
  • 1,629
  • 2
  • 16
  • 32
0

Not to mention one shouldn't be redirecting on errors -- you don't want to send HTTP 3xx headers there and you can have nasty redirect loops when things are really going down across the board.

Wyatt Barnett
  • 15,573
  • 3
  • 34
  • 53
  • There are lots of good reasons to redirect on an "error". For example, IIS treats 404s as errors, and there are many cases where you want a permanent or other redirect instead of responding with the 404, etc. – A.R. Apr 10 '15 at 18:49