c# - 如何在ASP.net MVC中显示IIS服务器的自定义错误消息-5

标签 c# .net asp.net-mvc error-handling asp.net-mvc-5

我是Web开发的新手,我需要在MVC-5应用程序中显示来自IIS服务器的自定义错误消息。例如“504网关超时错误”或“500错误”。我正在研究现有的代码。以下是我到目前为止所拥有的。

BaseController:

protected override void OnException(ExceptionContext filterContext)
        {
            if (!filterContext.ExceptionHandled)
            {
                filterContext.ExceptionHandled = true;
                Logger.Error("SimController", "OnException", "An exception has occurred.", filterContext.Exception);

                // this will cause the error to be displayed to the user..
                Response.StatusCode = 500;

                Response.StatusDescription = Constants.DISPLAYED_ERROR_MESSAGE_SHARED;

                if (filterContext.Exception.GetType() == typeof(ValidationException))
                { Response.StatusDescription += Constants.ADDITIONAL_DETAIL_ERROR_MESSAGE + filterContext.Exception.Message; }
                else
                { Response.StatusDescription += Constants.GENERIC_ERROR_MESSAGE; }
            }
        }

ErrorController:
 public ActionResult Index(string Message)
        {
            // We choose to use the ViewBag to communicate the error message to the view
            ViewBag.Message = Message;
            return View();
        }
        public ActionResult Oops(string Message)
        {
            // We choose to use the ViewBag to communicate the error message to the view
            ViewBag.Message = Message;
            return View();
        }

Web配置
 <system.web>
    <compilation debug="true" targetFramework="4.5.1" />
    <httpRuntime targetFramework="4.5" />
    <customErrors mode="On" defaultRedirect="~/ErrorPage/Oops">
      <error redirect="~/Error/Oops/404" statusCode="404" />
      <error redirect="~/Error/Oops/500" statusCode="500" />
    </customErrors>
  </system.web>

Global.asax
 public class FilterConfig
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            //filters.Add(new HandleErrorAttribute());
        }
    }



protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);

    }

我是编码的新手,所以我在这篇文章中投入了一切。那是吗?我仍然看不到500的自定义页面。

我模拟错误的方式是

家庭 Controller :
 public ActionResult Index()
 {
      return new HttpStatusCodeResult(500); 
      return View();
 }

我推荐的帖子:Custom Error

最佳答案

  • 您没有为ErrorController创建 View 。
  • Oops(string Message)将message作为参数。您在此处传递错误代码:redirect="~/Error/Oops/404"
  • 不要更改'Global.asax'文件。 <customErrors mode="On">就足够了。
  • 关于c# - 如何在ASP.net MVC中显示IIS服务器的自定义错误消息-5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41620162/

    相关文章:

    java - 使用 Lucene 的缺点是什么?

    c# - 使用 .net WebAPI 创建、更新、删除子实体

    c# - 如何在自定义 ModelBinder 中从 body 获取数据?

    c# - 使用 ASP.NET MVC 获取用户的本地路径

    jquery - JQM 弹出窗口内的提交按钮未点击 MVC 中的操作方法

    c# - 如何在 C# 中映射 HALF_PTR

    c# - FileStream.Flush() 和 FileStream.Flush(True) 有什么区别?

    c# - 将文件拖到桌面快捷方式 - 不在应用程序中打开文件

    c# - Visual Studio 2010 Entity Framework .edmx 不可编辑

    c# - Asp.net Web api2 模拟 webClient 调用