返回404页面没有重定向

Jor*_*ork 4 asp.net-core-mvc asp.net-core

我试图让我的404正常工作,但无法弄清楚如何使它正确.

最初我在我的Statup Configure方法中设置了以下内容:

app.UseMvc(routes =>
{
    // routing here
});

app.Use((context, next) =>
{
    context.Response.StatusCode = 404;
    return next();
});

app.UseStatusCodePagesWithRedirects("/error/{0}");
Run Code Online (Sandbox Code Playgroud)

哪个重定向到我显示错误的页面.但是状态代码是302 > 200.我设置/error/{code}动作以返回相关的状态代码,所以现在我有302 > 404(由于302重定向)使它看起来好像/error/404页面不存在(它做了).

我想要做的是返回没有重定向的错误页面,因此尝试请求/doesntexist将返回404并显示错误页面.

我尝试的另一件事是使用app.UseStatusCodePagesWithReExecute("/error/{0}");哪个返回404而不更改URL但只显示空白页而不是我的错误页

Joe*_*tte 11

在我的应用程序中,我这样做:

// custom 404 and error page - this preserves the status code (ie 404)
app.UseStatusCodePagesWithReExecute("/Home/Error/{0}");
Run Code Online (Sandbox Code Playgroud)

我的HomeController有这个动作方法

public IActionResult Error(int statusCode)
{
    if (statusCode == 404)
    {
        var statusFeature = HttpContext.Features.Get<IStatusCodeReExecuteFeature>();
        if (statusFeature != null)
        {
            log.LogWarning("handled 404 for url: {OriginalPath}", statusFeature.OriginalPath);
        }

    }
    return View(statusCode);
}
Run Code Online (Sandbox Code Playgroud)

我的观点是这样的:

@model int

@{
    switch (Model)
    {
        case 400:
            ViewData["Icon"] = "fa fa-ban text-danger";
            ViewData["Title"] = "Bad Request";
            ViewData["Description"] = "Your browser sent a request that this server could not understand.";
            break;
        case 401:
            ViewData["Icon"] = "fa fa-ban text-danger";
            ViewData["Title"] = "Unauthorized";
            ViewData["Description"] = "Sorry, but the page requires authentication.";
        break;
        case 403:
            ViewData["Icon"] = "fa fa-exclamation-circle text-danger";
            ViewData["Title"] = "Forbidden";
            ViewData["Description"] = "Sorry, but you don't have permission to access this page.";
        break;
        case 404:
            ViewData["Icon"] = "fa fa-exclamation-circle text-danger";
            ViewData["Title"] = "Page Not Found";
            ViewData["Description"] = "Sorry, but the page you were looking for can't be found.";
            break;
        case 500:
        default:
            ViewData["Icon"] = "fa fa-exclamation-circle text-danger";
            ViewData["Title"] = "Unexpected Error";
            ViewData["Description"] = "Well, this is embarrassing. An error occurred while processing your request. Rest assured, this problem has been logged and hamsters have been released to fix the problem.";
            break;
    }
}

<div class="jumbotron text-center">
    <header>
        <h1><span aria-hidden="true" class="@ViewData["Icon"]"></span> @ViewData["Title"]</h1>
    </header>
    <p>@ViewData["Description"]</p>
    <a class="btn btn-primary btn-lg" href="@Url.RouteUrl("/")"><span aria-hidden="true" class="fa fa-home"></span> Site Home</a>
</div>
Run Code Online (Sandbox Code Playgroud)

正如@khellang所提到的,中间件的顺序很重要,这应该在app.UseMvc之前