zl程序教程

您现在的位置是:首页 >  其他

当前栏目

MVC 5 错误异常处理 和 404 page 学习笔记

错误笔记异常学习MVC 处理 Page 404
2023-09-27 14:23:57 时间

参考 : 

 

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