加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-core – 返回404页面,不重定向

发布时间:2020-12-16 07:29:50 所属栏目:asp.Net 来源:网络整理
导读:我试图让我的404正常工作,但无法弄清楚如何使它正确. 最初我在我的Statup Configure方法中设置了以下内容: app.UseMvc(routes ={ // routing here});app.Use((context,next) ={ context.Response.StatusCode = 404; return next();});app.UseStatusCodePage
我试图让我的404正常工作,但无法弄清楚如何使它正确.

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

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

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

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

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

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

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

解决方法

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

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

我的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之前

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读