首页 > 解决方案 > app.UseStatusCodePagesWithReExecute("/Error/{0}"); 在 ASP.NET Core 2.2 中不工作

问题描述

我把它放在 Startup.cs 的 Configure 方法中

        app.UseMvc();

        app.UseStatusCodePagesWithReExecute("/Error/{0}");

然后我创建了一个这样的控制器:

[Route("Error/{statusCode}")]
    public IActionResult HandleErrorCode(int statusCode)
    {
        var statusCodeData = HttpContext.Features.Get<IStatusCodeReExecuteFeature>();

        switch (statusCode)
        {
            case 404:
                ViewBag.ErrorMessage = "Sorry the page you requested could not be found";
                ViewBag.RouteOfException = statusCodeData.OriginalPath;
                break;
            case 500:
                ViewBag.ErrorMessage = "Sorry something went wrong on the server";
                ViewBag.RouteOfException = statusCodeData.OriginalPath;
                break;
        }

        return View();
    }

当我导航到 localhost:port/doesnt-exist

叹息......我得到一个空白页。

标签: asp.net-coreasp.net-core-mvc

解决方案


空白页的原因是没有找到视图。因为我不再使用开发人员异常页面,所以我无法轻易意识到这一点。

 I used 

 View("~/Views/Shared/Error.cshtml")

代替

  View();

推荐阅读