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);
        }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM