使用系统默认的错误处理方式,能够减小一次重定向访问,而且正常提示了错误信息,并发送了错误码500,但应注意如下问题:css
1、须要修改web.confightml
<customErrors mode="On">
</customErrors>jquery
2、若是修改默认的Error.cshtml文件,不使用默认布局页或修改布局页,应引用下列文件web
@Styles.Render("~/Content/css")
@Scripts.Render("~/bundles/modernizr")
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/bootstrap")bootstrap
3、默认的HandleErrorAttribute只处理500错误,因此应重写此类并加入过滤器。写入日志。服务器
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; }
若是不使用过滤器,参考下面内容app
===========================================================================================================================================asp.net
使用HandleErrorAttribute后,找到了Error.cshtml,则此时异常已经被捕获处理,因此不会再次被Application_Error捕获处理。布局
因此啊,若是想使用Application_Error,就要
public class FilterConfig { public static void RegisterGlobalFilters(GlobalFilterCollection filters) { //filters.Add(new HandleErrorAttribute()); } }
=======================================================================================================================================================================================
当ASP.NET MVC程序出现了异常,怎么处理更加规范?
1. 最简单的方法是设置<customErrors/>节点
<customErrors>节点用于定义一些自定义错误信息的信息。此节点有Mode和defaultRedirect两个属性,其中defaultRedirect属性是一个可选属性,表示应用程序发生错误时重定向到的默认URL,若是没有指定该属性则显示通常性错误。Mode属性是一个必选属性,它有三个可能值,它们所表明的意义分别以下: On 表示在本地和远程用户都会看到自定义错误信息。 Off 禁用自定义错误信息,本地和远程用户都会看到详细的错误信息。 RemoteOnly 表示本地用户将看到详细错误信息,而远程用户将会看到自定义错误信息。
这里有必要说明一下本地用户和远程用户的概念。当咱们访问asp.net应用程时所使用的机器和发布asp.net应用程序所使用的机器为同一台机器时成为本地用户,反之则称之为远程用户。在开发调试阶段为了便于查找错误Mode属性建议设置为Off,而在部署阶段应将Mode属性设置为On或者RemoteOnly,以免这些详细的错误信息暴露了程序代码细节从而引来黑客的入侵。
<error>子节点
在<customErrors>节点下还包含有<error>子节点,这个节点主要是根据服务器的HTTP错误状态代码而重定向到咱们自定义的错误页面,注意要使<error>子节点下的配置生效,必须将<customErrors>节点节点的Mode属性设置为“On”。下面是一个例子:
<customErrors mode="On" defaultRedirect="GenericErrorPage.htm"> <error statusCode="403" redirect="403.htm" /> <error statusCode="404" redirect="404.htm" /> </customErrors>
在上面的配置中若是用户访问的页面不存在就会跳转到404.htm页面,若是用户没有权限访问请求的页面则会跳转到403.htm页面,403.htm和404.htm页面都是咱们本身添加的页面,咱们能够在页面中给出友好的错误提示。
2. 使用过滤器HandleErrorAttribute
随着ASP.NET MVC版本的更新,出现了HandleErrorAttribute,使用Filter以AOP的思想实现了针对于Action的异常处理。使用此Filter后,当程序中出现异常的时候,会去封装这些异常信息,而后路由自动转到该Controller对应的Error.cshtml中,若是此路径下没有改文件,则会到shared目录中寻找此文件。另一个相关的是在Global.asax中的protected void Application_Error(object sender, EventArgs e)方法,是捕捉异常的最后一道防线,也就是说,这是最高层次的异常捕获处理逻辑。使用HandleErrorAttribute后,找到了Error.cshtml,则此时异常已经被捕获处理,因此不会再次被Application_Error捕获处理。此外,能够经过@model HandleErrorInfo 在Error.cshtml中定制显示异常信息。有一点须要注意的是,HandleErrorAttribute是在customErrors基础之上的,若是想使用HandleErrorAttribute,customErrors的Mode必需要设置为On或RemoteOnly. 不然,HandleErrorAttribute将不起做用。
3. 自定义显示路径
若是遇到异常后不仅仅是要自定义异常显示,并且还须要进行日志记录。此时,能够经过继承扩展HandleErrorAttribute来实现新的Filter,而后在lobal.ascx中对全部的Action进行注册,filters.Add(new MyHandleErrorAttribute()).在这里详细说明一下另外一种方式。咱们能够写一个专门的Controller和Action来进行异常发生时的路由设置.
public class UtilityController : Controller { public ActionResult Error() { return View(); } public ActionResult PageNotFound() { return View(); } }
当出现异常的时候,把异常抛到最顶端,由Application_Error统一处理。这里的统一处理就包括,记录日志,从新进行页面定向等。