MVC 5 错误异常处理 和 404 page 学习笔记
参考 :
http://shiyousan.com/post/635838881238204198 (ASP.NET MVC 5 学习笔记:使用HandleErrorAttribute处理异常)
http://shiyousan.com/post/635833789557065314 (ASP.NET MVC实现IExceptionFilter接口编写自定义异常处理过滤器)
https://github.com/ASP-NET-MVC/aspnetwebstack/blob/master/src/System.Web.Mvc/HandleErrorAttribute.cs (源码)
http://www.codeproject.com/Articles/850062/Exception-handling-in-ASP-NET-MVC-methods-explaine (各种小方式)
Note : HandleErrorAttribute 只针对 MVC controller 出现异常时捕获 500 Error.
1. Web config 开启 customErrors,HandleErrorAttribute 是依赖这个的
<configuration> <system.web> <customErrors mode="On"></customErrors> </system.web> </configuration>
2. 创建一个 ErrorConfig.Register for Startup setup filter
public class ErrorConfig { public static void Register(GlobalFilterCollection filters) { filters.Add(new HandleErrorAttribute()); } }
3. Owin Startup.cs
public class Startup { public void Configuration(IAppBuilder app) { ErrorConfig.Register(GlobalFilters.Filters); } }
4.添加 Views/Shared/Error.cshtml
@model System.Web.Mvc.HandleErrorInfo @{ ViewBag.Title = "Error"; } <p>error message : @Model.Exception.Message</p> <h1 class="text-danger">share Error.</h1> <h2 class="text-danger">An error occurred while processing your request.</h2>
关于 404 page
方式太多了 /.\
refer :
http://benfoster.io/blog/aspnet-mvc-custom-error-pages (目前我是用这个方式)
http://dusted.codes/demystifying-aspnet-mvc-5-error-pages-and-error-logging
http://forums.asp.net/t/1957903.aspx?Not+able+to+handle+404+errors+on+an+ASP+NET+MVC+Why+
<configuration> <system.web> <customErrors mode="On" redirectMode="ResponseRewrite"> <error statusCode="404" redirect="~/Content/ErrorViews/404.aspx" /> </customErrors> </system.web> <system.webServer> <httpErrors errorMode="Custom" existingResponse="Auto"> <clear /> <error statusCode="404" path="Content\ErrorViews\404.html" responseMode="File" /> </httpErrors> </system.webServer> </configuration>
customeErrors 要用 ResponseRewrite , 而且一定要用 .aspx 来去调statuscode to 404
customErrors mode="RemoteOnly" <-发布后改称 RemoteOnly , On for localhost
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="404.aspx.cs" Inherits="ProjectMVC.Content.ErrorViews._404" %> <% Response.StatusCode = 404; %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"></form> 404 page aspx </body> </html>
404.aspx 重点是要加下面红色的代码
虽然上面设置了404 page, 但是如果使用 webapi IHttpActionResult 返回 NotFound 是不会受影响的,webapi 有自己的逻辑
题外话 :
401 要特别注意在使用 cookie authentication middleware 会影响到 webapi, 这个以后我会再开一篇来说明
refer :
http://brockallen.com/2013/10/27/using-cookie-authentication-middleware-with-web-api-and-401-response-codes/
http://slynetblog.blogspot.my/2014/03/preventing-302-redirect-for-aspnet.html
相关文章
- 关于React报Too many re-renders. React limits the number of renders to prevent an infinite错误的解决方案
- Linux学习笔记之passwd:Authentication token manipulation error_错误的解决办法
- Web应用开发人员最易犯的九个安全错误
- [C++]Linux之Ubuntu下编译C程序出现错误:“ stray ‘302'或者'240' in program”的解决方案
- Springboot+shiro配置笔记+错误小结
- cmd 中 输入 appium-doctor 弹出错误提示框
- Python中super()或object.__new__报TypeError: object.__new__() takes no arguments错误的解决方案
- 【错误记录】Java AWT 图形界面编程界面中出现中文乱码 ( Run/Debug Configurations 对话框 VM options 中设置 -Dfile.encoding=gbk )
- Android Studio Error—Gradle: 错误:编码 GBK 的不可映射字符的
- 【错误记录】编译 Android 版本的 ijkplayer 报错 ( ./init-android.sh: 第 37 行: cd: android/contrib/: 没有那个文件或目录 )
- 【错误记录】Android Studio 配置 AspectJ 报错 ( Failed to create Jar file C:xxxaspectjtools-1.8.10.jar. )
- terminate called without an active exception错误分析
- 编写合格的C代码(1):通过编译选项将特定警告视为错误
- 【Linux】symbol lookup error: undefined symbol + nm指令定位错误
- 【hadoop】 running beyond virtual memory错误原因及解决办法