zoukankan      html  css  js  c++  java
  • asp.net 重写OnException返回json或跳转新页面

            protected override void OnException(ExceptionContext filterContext)
            {
                // 此处进行异常记录,可以记录到数据库或文本,也可以使用其他日志记录组件。
                // 通过filterContext.Exception来获取这个异常。
                filterContext.ExceptionHandled = true;//组织web.config配置customerror处理
                string requestType = filterContext.HttpContext.Request.RequestType.ToString();//获取请求类型
                UrlHelper url = new UrlHelper(filterContext.RequestContext);
    
                //判断是否为get请求,如果为get请求,跳转指定页面,如果不是返回json
                if (requestType.ToUpper() == "GET")
                {
                    filterContext.Result = new RedirectResult(url.Action("error", "Error"));//跳转到新页面
                }
                else
                {
                    filterContext.Result = new JsonResult() { Data = new { errorcode = 2, message = filterContext.Exception.Message }, JsonRequestBehavior = JsonRequestBehavior.AllowGet };//返回json数据
                }
                // 执行基类中的OnException
                //base.OnException(filterContext);
            }
  • 相关阅读:
    快速排序
    优先队列
    堆排序
    树、二叉树基础
    分治法
    递归算法详细分析
    算法基础
    Linux文件系统详解
    fs/ext2/inode.c相关函数注释
    块设备的读流程分析
  • 原文地址:https://www.cnblogs.com/Chavezcn/p/7580624.html
Copyright © 2011-2022 走看看