【问题标题】:Custom Error Page in Production for .NET 5.0.NET 5.0 生产中的自定义错误页面
【发布时间】:2021-05-29 15:12:55
【问题描述】:

我正在尝试在我的 .NET 5.0 中实现一个自定义 404 页面,以便在 Web 应用程序处于生产状态时使用。我在 Startup.cs 中实现了以下内容;

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            } 
            else
            {
                app.UseStatusCodePagesWithRedirects("/Error/{0}");
            } 
            ...
}

链接到一个简单的ErrorController

public class ErrorController : Controller
    {
        [Route("/Error/{statusCode}")]
        public IActionResult HttpStatusCodeHandler(int statusCode)
        {
            switch(statusCode)
            {
                case 404:
                    //ViewBag.ErrorMessage("Resource could not be found.");
                    break;
            }
            return View("Error");
        }
    }

然后转到 Shared 中的 Error.cshtml

这不会删除默认的Status Code: 404; Not Found 页面,但如果我直接通过 url 转到localhost/Error/404,则该页面是可访问的。

这就是我记得使用以前版本的 .NET 实现这一点的方式,但现在我不确定我是否遗漏了什么,或者新的 .NET 5.0 是否有实现自定义 404 页面的新要求。

任何帮助将不胜感激。

编辑:launchSettings.json 个人资料:

"Proj_prod": {
            "commandName": "Project",
            "dotnetRunMessages": "true",
            "launchBrowser": true,
            "applicationUrl": "https://localhost:5001;http://localhost:5000",
            "environmentVariables": {
                "ASPNETCORE_ENVIRONMENT": "Production"
            }
        }

【问题讨论】:

  • 我相信你只是在开发模式下运行应用程序。代码没有问题
  • 您可以在 Properties\LaunchSettings.json 中将环境变量 ASPNETCORE_ENVIRONMENT 从 Development 更改为 Production 并再次测试。正如 Alexander Mokin 所说,这是因为您正在测试 int 开发环境。
  • @AlexanderMokin 抱歉没有提到我已经检查以确保 "ASPNETCORE_ENVIRONMENT" 设置为 "Production"。编辑了问题以包含 launchSettings 配置文件详细信息以防万一。
  • 将 ASPNETCORE_ENVIRONMENT 设置为生产并不能保证您正在运行 Proj_prod 配置。默认配置是 IISExpress。在 app.UseStatusCodePagesWithRedirects("/Error/{0}"); 行上放一个断点并检查你是否在启动时点击它。
  • @AlexanderMokin app.UseStatusCodePagesWithRedirects 上的断点在启动时被命中。以防万一,我也将 IISExpress 设置为 Production,它产生与 Proj_prod 相同的结果。

标签: asp.net-core asp.net-core-mvc .net-5 custom-error-pages


【解决方案1】:

我了解您正在运行您的项目IIS Express。为此,您无法按预期找到您的自定义错误页面。您应该在 profile 中的launchsettings.json 中使用"ASPNETCORE_ENVIRONMENT": "Production"。它可以解决您的问题。只需更改您的代码,如下所示。


//clarify

  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Production"   //add this
      }
    },
    "Proj_prod": {
      "commandName": "Project",
      "launchBrowser": true,
      "applicationUrl": "https://localhost:5001;http://localhost:5000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

【讨论】:

    【解决方案2】:

    首先检查您是否有正确的路线,或者您是否被重定向到/Error/404,然后您可以将用户重定向到自定义 404

     [Route("/Error/{statusCode}")]
        public IActionResult HttpStatusCodeHandler(int? statusCode)
        {
            switch(statusCode.Value)
            {
                case 404:
                    //ViewBag.ErrorMessage("Resource could not be found.");
                    //TempData["Message"] = "Resource could not be found."
                    return RedirectToAction("404");
                    break;
            }
            return View("Error");
        }
    

    PS:您也可以使用app.UseStatusCodePagesWithReExecute("/Home/Error", "?statusCode={0}"); 发送请求参数,而不是作为路由

    【讨论】:

    • 我的问题是,当我输入任何垃圾 url 时,我不会被重定向到 /Error/_404_,它只会保留在垃圾 url 中并输出 Status Code: 404; Not Found。我已经尝试了上述实现以防万一,但我的问题没有改变。 app.UseStatusCodeWithRedirect / app.UseStatusCodePagesWithReExecute 在启动时被点击。
    【解决方案3】:

    好的,有问题的实际代码按预期工作。一位工作伙伴在我没有注意到的配置之后添加了app.UseStatusCodePages();

    (是的,花了 14 天。我终于回到了这里,才注意到。)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多