[ת]ASP.NET MVCÒì³£´¦Àí·½°¸
Òì³£´¦ÀíÊÇÿһ¸öϵͳ¶¼±ØÐëÒªÓеŦÄÜ£¬ÓÈÆä¶ÔÓÚWebϵͳ¶øÑÔ£¬¼òµ¥¡¢Í³Ò»µÄÒì³£´¦ÀíģʽÓÈΪÖØÒª£¬µ±´òËãʹÓÃASP.NET MVCÀ´×öÏîĿʱ£¬µÚÒ»¸öÊý¾Ý¼ÈëÒ³Ãæ¾ÍÓöµ½ÁËÕâ¸öÎÊÌâ¡£
ÔÚ֮ǰµÄASP.NET WebFormÏîÄ¿ÖУ¬Ò»°ãÇé¿ö϶¼ÊÇÔÚApplication_Errorʼþ´¦ÀíÆ÷ºÍ ScriptManager_AsyncPostBackErrorʼþ´¦ÀíÆ÷ÀïÃæ½øÐУ¬ÔÚASP.NET MVCÖÐÓÃÕâÁ½ÖÖ·½·¨Ëƺõ¶¼²»ºÏÊÊÁË£¬¸Ã·ÅÔÚÄĶùÄØ£¿×ܲ»ÖÁÓÚÔÚÿ¸öActionÀïÃ涼·ÅÒ»¸ötry{...}catch{...}°É¡£
ÔÚScottGuµÄ²©¿ÍÀïÃæÌáµ½ÁËÒ»¸öÀࣺHandleErrorAttribute£¬ËƺõÊÇÓÃÓÚ´¦ÀíÒì³£µÄ£¬ÓÚÊÇʹÓÃHandleErrorAttributeÀ´×ö¸ö³¢ÊÔ£¬£¨ËµÃ÷£¬Èç¹ûʹÓÃÁ˸ÃÀàÐÍ£¬²¢ÇÒÏë°ÑÒì³£ÏÔʾÔÚ×ÔÒÑÖ¸¶¨µÄView£¬Ôò±ØÐëÔÚweb.configÀïÃæµÄ<system.web>½Úµã¼ÓÉÏ<customErrors mode="On" />£©·¢ÏÖHandleErrorµÄÈ·±È½ÏºÃÓ㬿ÉÒÔʹÓÃÆäViewÊôÐÔÖ¸¶¨Òì³£ºóÌøתµÄÒ³Ã棬¿ÉÒÔÕë¶Ô²»Í¬µÄÒì³£ÀàÐÍÌøµ½²»Í¬µÄÒì³£ÏÔʾView£¬¶øÇÒÒ²¿ÉÒÔ²»Ìøתµ½Òì³£ÏÔʾView,ÏÔʾµ½µ±Ç°View,Àý:
[HttpPost] [HandleError(View = "Create", ExceptionType = typeof(Exception))] public ActionResult Create(string someParam) { throw new Exception("oops..."); }
µ±Òì³£·¢Éúʱ£¬Ò³Ã滹»áÌø»Øµ½Create£¬Ö»ÊÇÕâÀïÓеãСÎÊÌ⣬Óû§ÔÚÒ³ÃæÉÏÊäÈëÁ˺ܶණÎ÷£¬ÄãÌáʾ¸öÒì³£²»ÖÁÓÚ°ÑËûÐÁÐÁ¿à¿àÊäÁË°ëÌìµÄ¶«Î÷¶¼Ã»ÓÐÁË°É£¬°ÑÕâÑùµÄÏîÄ¿ËͳöÈ¥£¬³ÙÔçÊÇÒª¸Ä»ØÀ´µÄ¡£
´ò¿ªHandleErrorAttributeµÄÔ´´úÂë¿ÉÒÔ¿´Æä¹Ø¼ü²¿·Ö£º
public virtual void OnException(ExceptionContext filterContext) { if (filterContext == null) { throw new ArgumentNullException("filterContext"); } if (filterContext.IsChildAction) { return; } // If custom errors are disabled, we need to let the normal ASP.NET exception handler // execute so that the user can see useful debugging information. if (filterContext.ExceptionHandled || !filterContext.HttpContext.IsCustomErrorEnabled) { return; } Exception exception = filterContext.Exception; // If this is not an HTTP 500 (for example, if somebody throws an HTTP 404 from an action method), // ignore it. if (new HttpException(null, exception).GetHttpCode() != 500) { return; } if (!ExceptionType.IsInstanceOfType(exception)) { return; } string controllerName = (string)filterContext.RouteData.Values["controller"]; string actionName = (string)filterContext.RouteData.Values["action"]; HandleErrorInfo model = new HandleErrorInfo(filterContext.Exception, controllerName, actionName); filterContext.Result = new ViewResult { ViewName = View, MasterName = Master, ViewData = new ViewDataDictionary<HandleErrorInfo>(model), TempData = filterContext.Controller.TempData }; filterContext.ExceptionHandled = true; filterContext.HttpContext.Response.Clear(); filterContext.HttpContext.Response.StatusCode = 500; // Certain versions of IIS will sometimes use their own error page when // they detect a server error. Setting this property indicates that we // want it to try to render ASP.NET MVC's error page instead. filterContext.HttpContext.Response.TrySkipIisCustomErrors = true; }
ÍƼöÐÅÏ¢
ÈÈÃÅÐÅÏ¢
- nohup: redirecting stderr to stdou....
- ʹÓÃlog_formatΪNginx·þÎñÆ÷ÉèÖøüÏêϸµÄÈÕÖ¾¸ñʽ
- jquery easyUI--dataGrid-Json
- [Ô´´]·ÂGoogle Reader¡¢ÐÂÀË΢²©¡¢ÌÚѶ΢²©µ....
- ÀûÓÃKeepalived+mysql¹¹½¨¸ß¿ÉÓÃMySQLË«Ö÷×Ô¶....
- Nginx+keepalivedʵÏÖ¸ºÔؾùºâºÍË«»úÈȱ¸¸ß¿ÉÓÃ
- jqueryʵÏÖÒ³Ãæ¼ÓÔؽø¶ÈÌõ
- Rolling cURL: PHP²¢·¢×î¼Ñʵ¼ù
- codeigniter ·ÓÉÖÕ¼«ÓÅ»¯(url rewrite)
- linuxÏÂÉèÖÃsshÎÞÃÜÂëµÇ¼
×î½ü¸üÐÂ
- Asp.net MVCÔ´Âë·ÖÎö--Model Validation(Se....
- .Net À¬»ø»ØÊÕ»úÖÆÔÀíºÍËã·¨£¨Ò»£©
- .netʵÌåнâ
- ·ÖÎöASP.NET¶ÁÈ¡XMLÎļþ4ÖÖ·½·¨
- »ñÈ¡ÍøÕ¾ËõÂÔͼ´úÂë
- Asp.net ¸ù¾ÝIPµØÖ·»ñÈ¡¿çÍø¶ÎmacµØÖ·º¯Êý¡¾ËѲء¿
- ¹ýÂËÃô¸Ð¹Ø¼ü×Ö¡£
- Éú³ÉÖ¸¶¨Î»ÊýµÄËæ»ú×Ö·û´®
- ¿Í»§¶Ë»ñÈ¡MACµØÖ·£¬µ½·þÎñÆ÷ÑéÖ¤
- ²½²½ÎªÓª .NETÈý²ã¼Ü¹¹½âÎö ¶þ¡¢Êý¾Ý¿âÉè¼Æ
ÆÀÂÛ