zoukankan      html  css  js  c++  java
  • 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 页面

  • 相关阅读:
    target runtime apache v6.0 not defined解决
    java.lang.AbstractMethodError: javax.servlet.jsp.JspFactory.getJspApplicationContext(Ljavax/servlet/ServletContext;)Ljavax/servlet/jsp/JspApplicationContext;
    The valid characters are defined in RFC 7230 and RFC 3986问题
    invalid END header解决方法
    You have more than one version of ‘org.apache.commons.logging.Log’ visible, which is not allowed问题解决
    Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986
    在eclipse中import java web项目时遇到的一些问题并将该项目通过tomcat发布
    java byte转string 涉及到字节流中有中文
    spring+mybatis框架搭建时遇到Mapped Statements collection does not contain value for...的错误
    试试看读一下Zepto源码
  • 原文地址:https://www.cnblogs.com/wilsonjw/p/3620658.html
Copyright © 2011-2022 走看看