asp.net mvc 错误处理

方法一:

1.写一个controller的子类。然后需要写错误日志报告的控制器继承这个类即可:
 
    public class BaseController : Controller
    {
        protected override void OnException(ExceptionContext filterContext)
        {
            Exception ex = filterContext.Exception;
            string filePath = System.Web.HttpContext.Current.Server.MapPath("/_error.txt");
            StreamWriter sw = System.IO.File.AppendText(filePath);
            sw.WriteLine("来源IP:" + System.Web.HttpContext.Current.Request.UserHostAddress);
            sw.WriteLine("异常时间:" + DateTime.Now.ToString());
            sw.WriteLine("异常页面:" + System.Web.HttpContext.Current.Request.Url);
            sw.WriteLine("异常信息:" + ex.Message);
            sw.WriteLine("异常来源:" + ex.Source);
            sw.WriteLine("堆栈信息:" + ex.StackTrace);
            sw.WriteLine("------------------------------");
            sw.WriteLine("");
            sw.Close();
            filterContext.ExceptionHandled = true;
            filterContext.Result = Redirect("Home/ErrorPages");//重定向到首页
        }
    }
 
方法二:
1.通过过滤器实现一个错误日志的特性:
 
    public class LogExceptionFilterAttribute : FilterAttribute, IExceptionFilter
    {
        public void OnException(ExceptionContext filterContext)
        {
            // 添加记录日志代码
            Exception ex = filterContext.Exception;
            string filePath = System.Web.HttpContext.Current.Server.MapPath("/_error.txt");
            StreamWriter sw = System.IO.File.AppendText(filePath);
            sw.WriteLine("来源IP:" + System.Web.HttpContext.Current.Request.UserHostAddress);
            sw.WriteLine("异常时间:" + DateTime.Now.ToString());
            sw.WriteLine("异常页面:" + System.Web.HttpContext.Current.Request.Url);
            sw.WriteLine("异常信息:" + ex.Message);
            sw.WriteLine("异常来源:" + ex.Source);
            sw.WriteLine("堆栈信息:" + ex.StackTrace);
            sw.WriteLine("------------------------------");
            sw.WriteLine("");
            sw.Close();
            filterContext.ExceptionHandled = true;
            filterContext.Result = new RedirectResult("Home/ErrorPages");//重定向到错误页面
        }
    }
 
2.如果是想给所有的控制器都写上错误报告,则需要在Global.asax.cs中的FilterConfig.RegisterGlobalFilters()添加全局的特性
 
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new LogExceptionFilterAttribute());
            filters.Add(new HandleErrorAttribute());
        }
 
3.如果只给制定的页面制定。这就可以直接在Action上面贴上标签就可以了
 
        [LogExceptionFilter]
        public ActionResult Index()
        {
            string a = "asdf";
            int b = Convert.ToInt32(a);
            return View();
        }
 
 
方法三:
1.如果不需要生成日志,错误直接跳转页面,就可以使用如下方法,在web.config的system.web下面添加 customErrors 标签。
 
    <customErrors mode="On" defaultRedirect="Home/ErrorPages">
      <error statusCode="403" redirect="Home/ErrorPages" />
      <error statusCode="404" redirect="Home/ErrorPages" />
      <error statusCode="500" redirect="Home/ErrorPages" />
    </customErrors>
 

Mode的值可以是Off、On、RemoteOnly,不同的值定义研发阶段或产品发布后的行为。

Mode值的使用场景:

 

  • On:开启自定义错误处理。
  • Off:关闭自定义错误处理,当发生异常时,就会看到ASP.NET的黄页信息。
  • RemoteOnly:如果在服务器上运行程序(http://localhost),当发生异常时,不会看到自定义异常信息,如果通过其他机器访问该程序,会看到自定义异常信息。该选项常用于开发人员调试程序,如果出现异常,开发人员可以通过本地访问来查看异常详细信息,而远程访问的用户看到的是自定义的异常。

  

注意:

    如果使用了HandleError特性,并且启用了CustomError,当有未处理的异常发生时,MVC在被执行的HttpRequest的上下文中查找”Error”视图(当前Controler对应的View文件夹中或Shared文件夹中),并呈现给用户。在这种情况下,CustomError的”defaultRedirect”和”redirect”属性会失效。注意:如果找不到Error视图,会使用”defaultRedirect”和”redirect”的定向。

 

    如果没有使用HandleError,并且启用了CustomError,当有未处理的异常发生时,会重定向到”defaultRedirect”和”redirect”属性指定的url,如上例的/Error/Unknown

     提示:ASP.N.NET MVC3中,默认对所有的Controller注册全局HandleError,因此不用担心应用程序中的Controller没有使用HandleError。在之前版本中没有全局过滤器,HandleError必须对每个action或controller手工定义。

 

    在web.config的CustomError中,也可以设置当异常出现重新定向到一个静态页面,如下:

     <customErrors mode="On" defaultRedirect="Custom404.htm">

   </customErrors>

 

    注意:静态页面需要放到web网站根目录下,否则无法定向到。

 
 
 
原文地址:https://www.cnblogs.com/oncoy/p/3636314.html