zoukankan      html  css  js  c++  java
  • 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网站根目录下,否则无法定向到。

     
     
     
  • 相关阅读:
    前端有关请求的相关内容axios
    有关浏览器异步请求数据的跨域问题
    Java环境的配置
    Css3中有关的 @media 媒体查询相关的知识
    Vue中实现异步加载的组件进行分割介绍
    Less的相关知识
    Vue框架中有关 computed的相关知识
    vue中如何在子组件添加类似于watch属性监听父组件数据,数据变化时子组件做出相应的动作
    JS的有关递归的知识点(数据无限级联的实现)
    JS中有关闭包的相关内容及介绍
  • 原文地址:https://www.cnblogs.com/oncoy/p/3636314.html
Copyright © 2011-2022 走看看