Return to page 404 without redirection

I am trying to get my 404s to work correctly, but cannot figure out how to do it right.

Initially, I installed the following in my Configure method:

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

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

app.UseStatusCodePagesWithRedirects("/error/{0}");

Which redirected to the page where I showed the error. However, there were status codes 302 > 200. I set the action /error/{code}to return the corresponding status code, so now I have 302 > 404one that (due to 302 redirection) makes it look like the page /error/404does not exist (which it does).

What I want to do is return the error page without redirecting, so trying to request /doesntexistwill return 404 and display the error page.

, , app.UseStatusCodePagesWithReExecute("/error/{0}");, 404 URL-, ,

+4
1

:

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

My 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);
}

:

@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>

@khellang, , app.UseMvc

+9

Source: https://habr.com/ru/post/1651536/


All Articles