mvc 全局错误处理_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > mvc 全局错误处理

mvc 全局错误处理

 2014/3/24 14:48:47  house11  博客园  我要评论(0)
  • 摘要:1publicstaticvoidAppErrorSet(HttpContextcontext,ControllererrorController)2{3varhttpContext=context;4varcurrentController="";5varcurrentAction="";6varcurrentRouteData=RouteTable.Routes.GetRouteData(newHttpContextWrapper(httpContext));78if
  • 标签:MVC 错误 全局
class="code_img_closed" src="/Upload/Images/2014032414/0015B68B3C38AA5B.gif" alt="" />logs_code_hide('15b8d597-ebf1-48b0-8af1-d3eee19c5ed1',event)" src="/Upload/Images/2014032414/2B1B950FA3DF188F.gif" alt="" />
 1 public static void AppErrorSet(HttpContext context, Controller errorController)
 2         {
 3             var httpContext = context;
 4             var currentController = " ";
 5             var currentAction = " ";
 6             var currentRouteData = RouteTable.Routes.GetRouteData(new HttpContextWrapper(httpContext));
 7 
 8             if (currentRouteData != null)
 9             {
10                 if (currentRouteData.Values["controller"] != null && !String.IsNullOrEmpty(currentRouteData.Values["controller"].ToString()))
11                 {
12                     currentController = currentRouteData.Values["controller"].ToString();
13                 }
14                 if (currentRouteData.Values["action"] != null && !String.IsNullOrEmpty(currentRouteData.Values["action"].ToString()))
15                 {
16                     currentAction = currentRouteData.Values["action"].ToString();
17                 }
18             }
19 
20             var ex = context.Server.GetLastError();
21             var controller = errorController;
22             var routeData = new RouteData();
23             var action = "Index";
24 
25             if (ex is HttpException)
26             {
27                 var httpEx = ex as HttpException;
28 
29                 switch (httpEx.GetHttpCode())
30                 {
31                     case 404:
32                         action = "NotFound";
33                         break;
34 
35                     case 401:
36                         action = "AccessDenied";
37                         break;
38                 }
39             }
40 
41             httpContext.ClearError();
42             httpContext.Response.Clear();
43             httpContext.Response.ContentType = "text/html";
44             httpContext.Response.StatusCode = ex is HttpException ? ((HttpException)ex).GetHttpCode() : 500;
45             httpContext.Response.TrySkipIisCustomErrors = true;
46 
47             routeData.Values["controller"] = "Error";
48             routeData.Values["action"] = action;
49 
50             controller.ViewData.Model = new HandleErrorInfo(ex, currentController, currentAction);
51             ((IController)controller).Execute(new RequestContext(new HttpContextWrapper(httpContext), routeData));
52         }
View Code
 1 public class ErrorController : Controller
 2   {
 3     public ActionResult Index()
 4     {
 5       return View();
 6     }
 7 
 8     public ActionResult NotFound()
 9     {
10       return View();
11     }
12 
13     public ActionResult AccessDenied()
14     {
15       return View();
16     }
17   }
View Code
 1 public class MvcApplication : System.Web.HttpApplication
 2     {
 3       
 4         protected void Application_Start()
 5         {
 6             //AreaRegistration.RegisterAllAreas();
 7 
 8             //RegisterGlobalFilters(GlobalFilters.Filters);
 9             //RegisterRoutes(RouteTable.Routes);
10             AreaRegistration.RegisterAllAreas();
11 
12             WebApiConfig.Register(GlobalConfiguration.Configuration);
13             FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
14             RouteConfig.RegisterRoutes(RouteTable.Routes);
15             BundleConfig.RegisterBundles(BundleTable.Bundles);
16             AuthConfig.RegisterAuth();
17 
18             //对象映射列表
19             AutoMapperConfig.MapperConfig();
20         }
21 
22         /// <summary>
23         /// 这里处理 mvcApplication 外的异常 mvc 框架边界外的
24         /// </summary>
25         /// <param name="sender"></param>
26         /// <param name="e"></param>
27         protected void Application_Error(object sender, EventArgs e)
28         {
29 
30             AppError.AppErrorSet(((MvcApplication)sender).Context,new ErrorController());
31             
32         }
33     }
View Code

在系统Application_Error 中定义的全局错误处理方法。这里可以处理 mvc 框架边界外的系统异常

没有 页面 302跳转的问题。404 500 http状态码也是正常显示。

errorController 中的view  在ie下 页面字节数 长度需要达到一定长度 ie才正常显示。不然会跳转到ie默认 404 页面

上一篇: 淘宝刷客工会揭秘:组织严密 分工明确 下一篇: 没有下一篇了!
发表评论
用户名: 匿名