• mvc 全局错误处理


     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 页面

  • 相关阅读:
    [Step By Step]SAP Visual intelligence连接到SAP HANA并访问视图 沧海
    SAP HANA中的用户拷贝(User Copy) 沧海
    在Cloudshare上使用PAL=〉系统检查及初始化 沧海
    SAP HANA学习资料中的基础表及数据SQL语句汇总 沧海
    [Step By Step]SAP HANA PAL多用户使用(单指数平滑法SINGLESMOOTH) 沧海
    [Step By Step]SAP HANA中创建层次分析视图(Hierarchy Analytic View) 沧海
    [Step By Step]SAP Visual Intelligence新增公式数据列(SAP HANA中提取数据) 沧海
    SAP HANA Education: Course & Certification Program 2013(SAP HANA认证考试) 沧海
    [Step By Step]SAP HANA中创建分析权限(Analytic Privilege) 沧海
    SAP HANA 存储过程中的IF While使用案例 沧海
  • 原文地址:https://www.cnblogs.com/wilsonjw/p/3620658.html
Copyright © 2020-2023  润新知