sportdog

导航

 

http://www.cnblogs.com/think8848/archive/2011/03/18/1987849.html

http://www.cnblogs.com/snowdream/archive/2009/04/24/1400730.html

思路:写个BaseController ,重写OnExceptiion方法.

当在ActionResult中抛出异常时(继承BaseController),OnException被执行并返回一个ViewData["Exception"]给当前页,由于页面上有可能需要其他的数据,页面就会报错。

页面上通过判断ViewData["Exception"] 是否为空来处理具体错误。

 

  protected override void OnException(ExceptionContext filterContext)
        {
            filterContext.Controller.ViewData["Exception"] = filterContext.Exception;
            filterContext.Result = new ViewResult() { ViewName = filterContext.Controller.ControllerContext.RouteData.Values["Action"].ToString(), ViewData = filterContext.Controller.ViewData };
            filterContext.ExceptionHandled = true;
            filterContext.HttpContext.Response.TrySkipIisCustomErrors = true; 
            base.OnException(filterContext);
        }

 

posted on 2014-01-13 11:32  sportdog  阅读(257)  评论(0编辑  收藏  举报