Skip to main content
Added TrySkipIisCustomErrors line
Source Link
Eduardo Molteni
  • 39.5k
  • 25
  • 145
  • 212

The code is taken from http://blogs.microsoft.co.il/blogs/shay/archive/2009/03/06/real-world-error-hadnling-in-asp-net-mvc-rc2.aspx and works in ASP.net MVC 1.0 as well

Here's how I handle http exceptions:

protected void Application_Error(object sender, EventArgs e) { Exception exception = Server.GetLastError(); // Log the exception. ILogger logger = Container.Resolve<ILogger>(); logger.Error(exception); Response.Clear(); HttpException httpException = exception as HttpException; RouteData routeData = new RouteData(); routeData.Values.Add("controller", "Error"); if (httpException == null) { routeData.Values.Add("action", "Index"); } else //It's an Http Exception, Let's handle it. { switch (httpException.GetHttpCode()) { case 404: // Page not found. routeData.Values.Add("action", "HttpError404"); break; case 500: // Server error. routeData.Values.Add("action", "HttpError500"); break; // Here you can handle Views to other error codes. // I choose a General error template default: routeData.Values.Add("action", "General"); break; } } // Pass exception details to the target error View. routeData.Values.Add("error", exception); // Clear the error on server. Server.ClearError(); // Avoid IIS7 getting in the middle Response.TrySkipIisCustomErrors = true; // Call target Controller and pass the routeData. IController errorController = new ErrorController(); errorController.Execute(new RequestContext( new HttpContextWrapper(Context), routeData)); } 

The code is taken from http://blogs.microsoft.co.il/blogs/shay/archive/2009/03/06/real-world-error-hadnling-in-asp-net-mvc-rc2.aspx and works in ASP.net MVC 1.0 as well

Here's how I handle http exceptions:

protected void Application_Error(object sender, EventArgs e) { Exception exception = Server.GetLastError(); // Log the exception. ILogger logger = Container.Resolve<ILogger>(); logger.Error(exception); Response.Clear(); HttpException httpException = exception as HttpException; RouteData routeData = new RouteData(); routeData.Values.Add("controller", "Error"); if (httpException == null) { routeData.Values.Add("action", "Index"); } else //It's an Http Exception, Let's handle it. { switch (httpException.GetHttpCode()) { case 404: // Page not found. routeData.Values.Add("action", "HttpError404"); break; case 500: // Server error. routeData.Values.Add("action", "HttpError500"); break; // Here you can handle Views to other error codes. // I choose a General error template default: routeData.Values.Add("action", "General"); break; } } // Pass exception details to the target error View. routeData.Values.Add("error", exception); // Clear the error on server. Server.ClearError(); // Call target Controller and pass the routeData. IController errorController = new ErrorController(); errorController.Execute(new RequestContext( new HttpContextWrapper(Context), routeData)); } 

The code is taken from http://blogs.microsoft.co.il/blogs/shay/archive/2009/03/06/real-world-error-hadnling-in-asp-net-mvc-rc2.aspx and works in ASP.net MVC 1.0 as well

Here's how I handle http exceptions:

protected void Application_Error(object sender, EventArgs e) { Exception exception = Server.GetLastError(); // Log the exception. ILogger logger = Container.Resolve<ILogger>(); logger.Error(exception); Response.Clear(); HttpException httpException = exception as HttpException; RouteData routeData = new RouteData(); routeData.Values.Add("controller", "Error"); if (httpException == null) { routeData.Values.Add("action", "Index"); } else //It's an Http Exception, Let's handle it. { switch (httpException.GetHttpCode()) { case 404: // Page not found. routeData.Values.Add("action", "HttpError404"); break; case 500: // Server error. routeData.Values.Add("action", "HttpError500"); break; // Here you can handle Views to other error codes. // I choose a General error template default: routeData.Values.Add("action", "General"); break; } } // Pass exception details to the target error View. routeData.Values.Add("error", exception); // Clear the error on server. Server.ClearError(); // Avoid IIS7 getting in the middle Response.TrySkipIisCustomErrors = true; // Call target Controller and pass the routeData. IController errorController = new ErrorController(); errorController.Execute(new RequestContext( new HttpContextWrapper(Context), routeData)); } 
Added text telling that the solution works in MVC 1.0
Source Link
Eduardo Molteni
  • 39.5k
  • 25
  • 145
  • 212

The code is taken from http://blogs.microsoft.co.il/blogs/shay/archive/2009/03/06/real-world-error-hadnling-in-asp-net-mvc-rc2.aspx and works in ASP.net MVC 1.0 as well

here'sHere's how I handle http exceptions:

protected void Application_Error(object sender, EventArgs e) { Exception exception = Server.GetLastError(); // Log the exception. ILogger logger = Container.Resolve<ILogger>(); logger.Error(exception); Response.Clear(); HttpException httpException = exception as HttpException; RouteData routeData = new RouteData(); routeData.Values.Add("controller", "Error"); if (httpException == null) { routeData.Values.Add("action", "Index"); } else //It's an Http Exception, Let's handle it. { switch (httpException.GetHttpCode()) { case 404: // Page not found. routeData.Values.Add("action", "HttpError404"); break; case 500: // Server error. routeData.Values.Add("action", "HttpError500"); break; // Here you can handle Views to other error codes. // I choose a General error template default: routeData.Values.Add("action", "General"); break; } } // Pass exception details to the target error View. routeData.Values.Add("error", exception); // Clear the error on server. Server.ClearError(); // Call target Controller and pass the routeData. IController errorController = new ErrorController(); errorController.Execute(new RequestContext( new HttpContextWrapper(Context), routeData)); } 

hope this helps.

Shay

The code is taken from http://blogs.microsoft.co.il/blogs/shay/archive/2009/03/06/real-world-error-hadnling-in-asp-net-mvc-rc2.aspx

here's how I handle http exceptions:

protected void Application_Error(object sender, EventArgs e) { Exception exception = Server.GetLastError(); // Log the exception. ILogger logger = Container.Resolve<ILogger>(); logger.Error(exception); Response.Clear(); HttpException httpException = exception as HttpException; RouteData routeData = new RouteData(); routeData.Values.Add("controller", "Error"); if (httpException == null) { routeData.Values.Add("action", "Index"); } else //It's an Http Exception, Let's handle it. { switch (httpException.GetHttpCode()) { case 404: // Page not found. routeData.Values.Add("action", "HttpError404"); break; case 500: // Server error. routeData.Values.Add("action", "HttpError500"); break; // Here you can handle Views to other error codes. // I choose a General error template default: routeData.Values.Add("action", "General"); break; } } // Pass exception details to the target error View. routeData.Values.Add("error", exception); // Clear the error on server. Server.ClearError(); // Call target Controller and pass the routeData. IController errorController = new ErrorController(); errorController.Execute(new RequestContext( new HttpContextWrapper(Context), routeData)); } 

hope this helps.

Shay

The code is taken from http://blogs.microsoft.co.il/blogs/shay/archive/2009/03/06/real-world-error-hadnling-in-asp-net-mvc-rc2.aspx and works in ASP.net MVC 1.0 as well

Here's how I handle http exceptions:

protected void Application_Error(object sender, EventArgs e) { Exception exception = Server.GetLastError(); // Log the exception. ILogger logger = Container.Resolve<ILogger>(); logger.Error(exception); Response.Clear(); HttpException httpException = exception as HttpException; RouteData routeData = new RouteData(); routeData.Values.Add("controller", "Error"); if (httpException == null) { routeData.Values.Add("action", "Index"); } else //It's an Http Exception, Let's handle it. { switch (httpException.GetHttpCode()) { case 404: // Page not found. routeData.Values.Add("action", "HttpError404"); break; case 500: // Server error. routeData.Values.Add("action", "HttpError500"); break; // Here you can handle Views to other error codes. // I choose a General error template default: routeData.Values.Add("action", "General"); break; } } // Pass exception details to the target error View. routeData.Values.Add("error", exception); // Clear the error on server. Server.ClearError(); // Call target Controller and pass the routeData. IController errorController = new ErrorController(); errorController.Execute(new RequestContext( new HttpContextWrapper(Context), routeData)); } 
Formatted the code section
Source Link
Kieron
  • 27.2k
  • 16
  • 81
  • 124

protected void Application_Error(object sender, EventArgs e) {

Exception exception = Server.GetLastError();

// Log the exception.

ILogger logger = Container.Resolve();

logger.Error(exception);

Response.Clear();

HttpException httpException = exception as HttpException;

RouteData routeData = new RouteData();

routeData.Values.Add("controller", "Error");

if (httpException == null)

{

protected void Application_Error(object sender, EventArgs e) { Exception exception = Server.GetLastError(); // Log the exception. ILogger logger = Container.Resolve<ILogger>(); logger.Error(exception); Response.Clear(); HttpException httpException = exception as HttpException; RouteData routeData = new RouteData(); routeData.Values.Add("controller", "Error"); if (httpException == null) {  routeData.Values.Add("action", "Index"); 

}

else //It's an Http Exception, Let's handle it.

{

switch (httpException.GetHttpCode())

{

 } else //It's an Http Exception, Let's handle it. { switch (httpException.GetHttpCode()) { case 404:     // Page not found.     routeData.Values.Add("action", "HttpError404");     break;     case 500:     // Server error.     routeData.Values.Add("action", "HttpError500");     break;   // Here you can handle Views to other error codes.     // I choose a General error template      default:     routeData.Values.Add("action", "General");     break; } } // Pass exception details to the target error View. routeData.Values.Add("error", exception); // Clear the error on server. Server.ClearError(); // Call target Controller and pass the routeData. IController errorController = new ErrorController();  errorController.Execute(new RequestContext(    new HttpContextWrapper(Context), routeData)); } 

}

// Pass exception details to the target error View.

routeData.Values.Add("error", exception);

// Clear the error on server.

Server.ClearError();

// Call target Controller and pass the routeData.

IController errorController = new ErrorController();

errorController.Execute(new RequestContext(

new HttpContextWrapper(Context), routeData));

}

protected void Application_Error(object sender, EventArgs e) {

Exception exception = Server.GetLastError();

// Log the exception.

ILogger logger = Container.Resolve();

logger.Error(exception);

Response.Clear();

HttpException httpException = exception as HttpException;

RouteData routeData = new RouteData();

routeData.Values.Add("controller", "Error");

if (httpException == null)

{

 routeData.Values.Add("action", "Index"); 

}

else //It's an Http Exception, Let's handle it.

{

switch (httpException.GetHttpCode())

{

case 404:   // Page not found.   routeData.Values.Add("action", "HttpError404");   break;   case 500:   // Server error.   routeData.Values.Add("action", "HttpError500");   break; // Here you can handle Views to other error codes.   // I choose a General error template    default:   routeData.Values.Add("action", "General");   break; } 

}

// Pass exception details to the target error View.

routeData.Values.Add("error", exception);

// Clear the error on server.

Server.ClearError();

// Call target Controller and pass the routeData.

IController errorController = new ErrorController();

errorController.Execute(new RequestContext(

new HttpContextWrapper(Context), routeData));

}

protected void Application_Error(object sender, EventArgs e) { Exception exception = Server.GetLastError(); // Log the exception. ILogger logger = Container.Resolve<ILogger>(); logger.Error(exception); Response.Clear(); HttpException httpException = exception as HttpException; RouteData routeData = new RouteData(); routeData.Values.Add("controller", "Error"); if (httpException == null) {  routeData.Values.Add("action", "Index");  } else //It's an Http Exception, Let's handle it. { switch (httpException.GetHttpCode()) { case 404:   // Page not found.   routeData.Values.Add("action", "HttpError404");   break;   case 500:   // Server error.   routeData.Values.Add("action", "HttpError500");   break;   // Here you can handle Views to other error codes.   // I choose a General error template   default:   routeData.Values.Add("action", "General");   break; } } // Pass exception details to the target error View. routeData.Values.Add("error", exception); // Clear the error on server. Server.ClearError(); // Call target Controller and pass the routeData. IController errorController = new ErrorController();  errorController.Execute(new RequestContext(    new HttpContextWrapper(Context), routeData)); } 
Source Link
Shay Jacoby
  • 2.9k
  • 1
  • 17
  • 6
Loading