zoukankan      html  css  js  c++  java
  • ASP.net MVC4/MVC5 错误处理及设置

    很无聊的记录下。首先IIS 7以上的版本测试才成功

    首先

    //全局ErrorHandler  
      public static void RegisterGlobalFilters(GlobalFilterCollection filters)
            {
                filters.Add(new HandleErrorAttribute());
    
                filters.Add(new VcodeFilterAttribute());
            }

    然后MVC web.config 设置如下

    <!-- 放到system.webServer -->
      <httpErrors errorMode="Custom" existingResponse="Replace">
          <remove statusCode="404"/>
          <remove statusCode="500"/>
          <error statusCode="404" path="/Error/Error404" responseMode="ExecuteURL"/>
          <error statusCode="500" path="/Error/Error500" responseMode="ExecuteURL"/>
        </httpErrors>
    

      确保它是关闭的。

      <customErrors mode="Off" />

    然后有个ErrorController。

     public class ErrorController : Controller
        {
           
            public ActionResult Error404()
            {
                return View();
            }
    
            public ActionResult Error500()
            {
                return View();
            }
    
        }

    View 中,喜欢怎么写就怎么写。当然要设置一下StatusCode,否则全部返回200

     @{ Response.StatusCode = 404;}

    当然也可以在controller里面设置

    日志可以写在global里面

        protected void Application_Error(object sender, EventArgs e)
            {
                var exception = Server.GetLastError();
              //log exception
    }
  • 相关阅读:
    【hive】null值判断
    【hive】where使用注意的问题
    【hive】关于浮点数比较的问题
    【hive】在alter修改元数据的时候报错 mismatched input 'xxxxx' expecting KW_EXCHANGE
    破解诅咒心法
    泡妞心法
    awk高级
    排除故障的总结
    机房运维相关面试题
    统计流入流出流量
  • 原文地址:https://www.cnblogs.com/fantasylu/p/6294164.html
Copyright © 2011-2022 走看看