通过BaseControl实现自定义错误页面
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.Mvc; 6 using System.Web.Routing; 7 8 namespace xxx.xxx.Dashboard.Web.Areas.Desktop.Controllers 9 { 10 public class BaseController : Controller 11 { 12 protected override void OnActionExecuting(ActionExecutingContext filterContext) 13 { 14 if (Session["userInfo"] == null) 15 { 16 if (filterContext.HttpContext.Request.IsAjaxRequest()) 17 { 18 filterContext.Result = new HttpStatusCodeResult(499); 19 filterContext.HttpContext.Response.Write("/Desktop/Login/Index"); 20 return; 21 } 22 else 23 { 24 filterContext.Result = new RedirectResult("/Desktop/Login/Index"); 25 return; 26 } 27 } 28 base.OnActionExecuting(filterContext); 29 } 30 31 protected override void OnException(ExceptionContext filterContext) 32 { 33 string controllerName = (string)filterContext.RouteData.Values["controller"]; 34 string actionName = (string)filterContext.RouteData.Values["action"]; 35 HandleErrorInfo info = new HandleErrorInfo(filterContext.Exception, controllerName, actionName); 36 37 Logging.LogService.Error(String.Format("出错时间:{0},错误信息:{1},Controller:{2},错误源:{3},堆栈信息:{4}", 38 DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), 39 filterContext.Exception.Message, 40 filterContext.Controller, 41 filterContext.Exception.Source, 42 filterContext.Exception.StackTrace 43 )); 44 45 base.OnException(filterContext); 46 filterContext.Result = new ViewResult() 47 { 48 ViewName = "/Views/Shared/Error.cshtml",//如果有Area,Error也是最外层的Shared文件夹 49 ViewData = new ViewDataDictionary<HandleErrorInfo>(info) 50 }; 51 filterContext.ExceptionHandled = true;//必须加这行,才会触发; 52 } 53 54 public ActionResult Error() 55 { 56 return View(); 57 } 58 } 59 }
1 @model HandleErrorInfo 2 @{ 3 Layout = null; 4 } 5 <!DOCTYPE html> 6 <html> 7 <head> 8 <meta charset="utf-8"> 9 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 10 <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" /> 11 <title>错误</title> 12 <style type="text/css"> 13 html, 14 body { 15 height: 100%; 16 } 17 18 body { 19 background-color: #f2f2f2; 20 color: #444; 21 font: 12px/1.5 'Helvetica Neue', Arial, Helvetica, sans-serif; 22 /*background: url("../../Content/Error/blueprint.png") repeat 0 0;*/ 23 } 24 25 div.da-wrapper { 26 width: 100%; 27 height: auto; 28 min-height: 100%; 29 position: relative; 30 min-width: 320px; 31 } 32 33 div.da-wrapper .da-container { 34 width: 96%; 35 margin: auto; 36 } 37 38 div.da-content { 39 clear: both; 40 padding-bottom: 58px; 41 } 42 43 @@media only screen and (max-width:480px) { 44 div.da-content { 45 margin-top: auto; 46 } 47 } 48 49 div.da-error-wrapper { 50 width: 320px; 51 padding: 0; 52 margin: auto; 53 position: relative; 54 } 55 56 div.da-error-wrapper .da-error-heading { 57 color: #e15656; 58 text-align: center; 59 font-size: 24px; 60 font-family: Georgia, "Times New Roman", Times, serif; 61 } 62 63 @@-webkit-keyframes error-swing { 64 0% { 65 -webkit-transform: rotate(1deg); 66 } 67 68 100% { 69 -webkit-transform: rotate(-2deg); 70 } 71 } 72 73 @@-moz-keyframes error-swing { 74 0% { 75 -moz-transform: rotate(1deg); 76 } 77 78 100% { 79 -moz-transform: rotate(-2deg); 80 } 81 } 82 83 @@keyframes error-swing { 84 0% { 85 transform: rotate(1deg); 86 } 87 88 100% { 89 transform: rotate(-2deg); 90 } 91 } 92 93 div.da-error-wrapper .da-error-code { 94 width: 320px; 95 height: 300px; 96 padding: 120px 16px 0 16px; 97 position: relative; 98 margin: auto; 99 margin-bottom: 20px; 100 z-index: 5; 101 line-height: 1; 102 font-size: 32px; 103 text-align: center; 104 background: url("../../Content/Error/error-hanger.png") no-repeat center center; 105 -webkit-transform-origin: center top; 106 -moz-transform-origin: center top; 107 transform-origin: center top; 108 -webkit-animation: error-swing infinite 2s ease-in-out alternate; 109 -moz-animation: error-swing infinite 2s ease-in-out alternate; 110 animation: error-swing infinite 2s ease-in-out alternate; 111 } 112 113 div.da-error-wrapper .da-error-code .tip { 114 padding-top: 5px; 115 color: #e15656; 116 } 117 118 div.da-error-wrapper .da-error-code .tip2 { 119 padding-top: 15px; 120 font-size: 16px; 121 } 122 123 div.da-error-wrapper .da-error-code .tip3 { 124 padding-top: 20px; 125 font-size: 16px; 126 color: #e15656; 127 } 128 129 div.da-error-wrapper .da-error-pin { 130 width: 38px; 131 height: 38px; 132 display: block; 133 margin: auto; 134 margin-bottom: -27px; 135 z-index: 10; 136 position: relative; 137 background: url("../../Content/Error/error-pin.png") no-repeat center center; 138 } 139 140 p { 141 margin: 0; 142 padding: 0; 143 } 144 </style> 145 <title>错误页面</title> 146 </head> 147 <body> 148 <div class="da-wrapper"> 149 <div class="da-content"> 150 <div class="da-container clearfix"> 151 <div class="da-error-wrapper"> 152 <div class="da-error-pin"></div> 153 <div class="da-error-code"> 154 <p class="tip">异常</p> 155 @if (Model.Exception.Message.Contains("的对象无法转换为类型")) 156 { 157 string temp = @Model.Exception.Message + " 请检查数据库表字段类型是否更改..."; 158 <p class="tip2">Message:@temp</p> 159 } 160 else 161 { 162 <p class="tip2">Message:@Model.Exception.Message</p> 163 } 164 <p class="tip3">Controller: @Model.ControllerName / Action: @Model.ActionName</p> 165 <h1 class="da-error-heading">Sorry, Source: @Model.Exception.Source ,请处理后再试...</h1> 166 </div> 167 </div> 168 </div> 169 </div> 170 </div> 171 </body> 172 </html>