Last active
August 2, 2016 19:25
-
-
Save bmcdavid/152cb2f1fd26e1796430f9c4a7a263a2 to your computer and use it in GitHub Desktop.
Handle errors in global.asax
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protected void Application_Error(object sender, EventArgs e) | |
{ | |
Exception exception = Server.GetLastError(); | |
HttpException httpException = exception as HttpException; | |
var configuration = WebConfigurationManager.OpenWebConfiguration("~/Web.config"); | |
CustomErrorsSection customErrors = (CustomErrorsSection)configuration.GetSection("system.web/customErrors"); | |
// if redirect mode | |
if (customErrors != null && | |
( | |
customErrors.Mode == CustomErrorsMode.Off || | |
customErrors.RedirectMode == CustomErrorsRedirectMode.ResponseRedirect || | |
(customErrors.RedirectMode == CustomErrorsRedirectMode.ResponseRewrite && customErrors.Mode == CustomErrorsMode.RemoteOnly && Request.IsLocal) | |
) | |
) | |
{ | |
return; | |
} | |
/// Handle rewrite mode | |
IHtmlErrorPageHelper errorPages = null; | |
try | |
{ | |
errorPages = EPiServer.ServiceLocation.ServiceLocator.Current.GetInstance<IHtmlErrorPageHelper>(); | |
} | |
catch { } | |
if (errorPages == null) | |
return; | |
RouteData routeData = new RouteData(); | |
string controller = string.Empty, action = string.Empty; | |
if (httpException == null) // default to 500 | |
{ | |
controller = errorPages.ServerErrorControllerName; | |
action = errorPages.ServerErrorControllerAction; | |
} | |
else | |
{ | |
switch (httpException.GetHttpCode()) | |
{ | |
case 403: | |
// Not Authorized. | |
controller = errorPages.ForbiddenControllerName; | |
action = errorPages.ForbiddenControllerAction; | |
break; | |
case 404: | |
// Page not found. | |
controller = errorPages.NotFoundControllerName; | |
action = errorPages.NotFoundControllerAction; | |
break; | |
case 500: | |
default: | |
// Server error. | |
controller = errorPages.ServerErrorControllerName; | |
action = errorPages.ServerErrorControllerAction; | |
break; | |
} | |
} | |
if (string.IsNullOrWhiteSpace(controller) || string.IsNullOrWhiteSpace(action)) | |
return; | |
IController errorController = null; | |
RequestContext context = new RequestContext(new HttpContextWrapper(Context), routeData); | |
try | |
{ | |
// set route controller, action, and exception | |
routeData.Values.Add("controller", controller); | |
routeData.Values.Add("action", action); | |
routeData.Values.Add("error", exception); | |
// Create target Controller and pass the routeData. | |
IControllerFactory factory = ControllerBuilder.Current.GetControllerFactory(); | |
errorController = factory.CreateController(context, controller); | |
} | |
catch | |
{ | |
return; // allow defaults to occur | |
} | |
// Clear the error on server. | |
Server.ClearError(); | |
// Avoid IIS7 getting in the middle | |
Response.TrySkipIisCustomErrors = true; | |
// clear the content | |
Response.Clear(); | |
// execute new content | |
errorController.Execute(context); | |
// Call target Controller and pass the routeData. | |
//IController errorHandler = new WSOL.Web.Controllers.ErrorController(); | |
//errorHandler.Execute(new RequestContext(new HttpContextWrapper(Context), routeData)); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace WSOL.EPiServerCms.Web.Interfaces | |
{ | |
// | |
// Summary: | |
// Define Routes, controllers and actions | |
public interface IHtmlErrorPageHelper | |
{ | |
// | |
// Summary: | |
// Forbidden controller action | |
string ForbiddenControllerAction { get; } | |
// | |
// Summary: | |
// Forbidden controller name | |
string ForbiddenControllerName { get; } | |
// | |
// Summary: | |
// Forbidden controller route | |
string ForbiddenControllerRoute { get; } | |
// | |
// Summary: | |
// Not found controller action | |
string NotFoundControllerAction { get; } | |
// | |
// Summary: | |
// Not found controller name | |
string NotFoundControllerName { get; } | |
// | |
// Summary: | |
// Not found route | |
string NotFoundControllerRoute { get; } | |
// | |
// Summary: | |
// Server error controller | |
string ServerErrorControllerAction { get; } | |
// | |
// Summary: | |
// Server error controller name | |
string ServerErrorControllerName { get; } | |
// | |
// Summary: | |
// Server error route | |
string ServerErrorControllerRoute { get; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment