2

For error handling, I have added the following code to Web.config:

<system.web>
    <customErrors mode="On" defaultRedirect="~/Error.cshtml?">
    </customErrors>
<system.web>

And in Global.asax.cs:

void Application_Error(object sender, EventArgs e)
{
    // Code that runs when an unhandled error occurs
    Response.Redirect("/Error.cshtml?");
}

However, when the redirect to /Error.cshtml? occurs, there is an error 'This page has a redirect loop'

I've searched through other similar questions (such as How to solve Redirect Loop). However, I have no other routes calling Error.cshtml -- so does anyone have any idea what is causing this loop and how I can work around it?

EDIT: There are multiple errors occurring, and therefore the error page redirect is involved in a redirect loop. Does anyone know of a way to only call Response.Redirect("~/Error.cshtml") once?

EDIT2: If I change the redirect url to a random url outside of the project (such as https://www.facebook.com/), the customError redirect works properly. Still looking for a way to redirect to project page without infinitely looping through the error

Community
  • 1
  • 1
suhMAN
  • 179
  • 1
  • 4
  • 13
  • What do you have in Error.cshtml? – rageit Mar 27 '14 at 14:30
  • I just have some

    test text

    type of markup, nothing that should be causing any sort of loop

    – suhMAN Mar 27 '14 at 14:34
  • @suhMAN, are you sure no errors occur after you call `Response.Redirect`? – Andrei Mar 27 '14 at 14:36
  • I just tested, and it does look like I continue to end up in the Application_Error function. I found the Error.cshtml actually uses @model System.Web.Mvc.HandleErrorInfo -- would this have any effect? – suhMAN Mar 27 '14 at 14:43
  • Probably a 404 trying to retrieve the error page. Try ~/Error.cshtml – Dave Mar 27 '14 at 14:45
  • @Dave I use ~/Error.cshtml? because of this: http://stackoverflow.com/questions/4222226/how-to-prevent-aspxerrorpath-passed-as-a-query-string-in-asp-net-error-pages-t – suhMAN Mar 27 '14 at 14:48
  • Right, so make sure you use the tilde(~) in your global.asax redirect. – Dave Mar 27 '14 at 14:50
  • The problem is that Application_Error is being called in a loop (I have no idea why yet) -- anyone have a workaround so that if this loop occurs, the response.redirect is called only once and the loop ends? – suhMAN Mar 27 '14 at 14:57

2 Answers2

0

Try adding:

Server.ClearError();

first to clear the existing error before redirecting.

Brian Mains
  • 50,520
  • 35
  • 148
  • 257
0

Add redirectMode="ResponseRewrite" to customErrors section.

Ivan Doroshenko
  • 944
  • 7
  • 13