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
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 百万级群聊的设计实践
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程
· .NET 10 首个预览版发布,跨平台开发与性能全面提升
· 《HelloGitHub》第 107 期
· 从文本到图像:SSE 如何助力 AI 内容实时呈现?(Typescript篇)