3

I am catching unhandled errors using the Application_Error method within my Global.asax file, this is what I have so far:

 protected void Application_Error(object sender, EventArgs e)
    {
        Exception exception = Server.GetLastError();
        Response.Clear();

        HttpException httpException = exception as HttpException;

        if(httpException != null)
        {
            string action;

            switch(httpException.GetHttpCode())
            {
                case 404:
                    action = "404";
                    break;
                case 500:
                    action = "500";
                    break;

                default:
                    action = "Other";
                    break;
            }

              Server.ClearError();

              Response.Redirect(String.Format("~/Error/?error=" + action + "&message=" + exception.Message));
        }
    }

However I really don't like the idea of redirecting the user to error page, infact I would like the URL to remain the same.

For example when a page doesn't exist it shouldn't redirect to the path in the URL, instead it should remain on the same page but still display an error.

Does anyone know how to do this?

KTOV
  • 559
  • 3
  • 14
  • 39

1 Answers1

0

My solution redirects much like yours, but depending on how you deploy I would do this by customising the error pages themselves in IIS.

Check Out this answered question

Community
  • 1
  • 1
Steve Newton
  • 1,046
  • 1
  • 11
  • 28
  • It sounds so simple but yet so hard. All I know that's got to be done is in the Application_Error render the error view? Rather than redirect – KTOV May 13 '16 at 09:50