【问题标题】:Asp.net Core MVC UseStatusCodePagesWithRedirects localhost redirect too many timesAsp.net Core MVC UseStatusCodePagesWithRedirects localhost 重定向太多次
【发布时间】:2016-07-29 13:03:36
【问题描述】:

不确定我是否遗漏了一些明显的东西,或者这可能是一个错误。

在startup.cs,方法Configure,我有`UseStatusCodePagesWithRedirects'。我最终放置了一个静态文件,希望它会有所作为。之前我在 Home 控制器中使用错误方法,但最终结果是一样的。

这是我的完整配置方法:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            loggerFactory.AddConsole(Configuration.GetSection("Logging"));
            loggerFactory.AddDebug();

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseBrowserLink();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }

            app.UseStatusCodePagesWithRedirects("~/errors/{0}.html");
            app.UseStaticFiles();

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });
        }

我有一个error目录,其中包含一个静态 404.html 文件。

如果我故意创建 404 错误,例如在 URL 中的“Home”之后键入“joe”,我会得到 ​​p>

The localhost page isn’t working

localhost redirected you too many times.

在 Edge 中使用 F12/Network 检查,我看到它被重定向到正确的页面,但次数太多了。

所以,我想我的问题是:一旦点击了正确的错误页面,我该如何更改响应代码?但我觉得我不应该这样做。

提前感谢您帮助我了解这里发生的事情。

顺便说一句,如果我简单地输入app.UseStatusCodePages();,它确实可以工作,但这很丑......

【问题讨论】:

  • 目录errorerrors和code一样吗?
  • "我有一个错误目录,只有一个静态 404.html 文件。"这个目录在wwroot里面吗?
  • 目录错误,但不在 wwwroot 内!让我现在改一下,看看能不能解决。
  • 这就是问题所在,StaticFiles 中间件试图在那里找到,并再次抛出 404
  • 好的,它可以工作,在 wwwroot 中移动目录和文件。我更喜欢使用控制器和视图,但是由于默认的异常处理程序已经使用 Home/Error,我想我必须自己编写来处理这些页面错误。

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


【解决方案1】:

按照本网站的内容:

https://andrewlock.net/re-execute-the-middleware-pipeline-with-the-statuscodepages-middleware-to-create-custom-error-pages/

你必须在你的 Startup.cs 里面放->Configure 必须放这个:

        // Will give error status code
        //app.UseStatusCodePages();
        //app.UseStatusCodePagesWithRedirects("/Error/{0}");
        app.UseStatusCodePagesWithReExecute("/Home/Error", "?statusCode={0}");

那么你必须在你的 Home Controller 中加入以下代码:

    public IActionResult Error(int? statusCode = null)
    {
        if (statusCode.HasValue)
        {
            if (statusCode == 404 || statusCode == 500)
            {
                var viewName = statusCode.ToString();
                return View(viewName);
            }
        }

        return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
    }

最后,您将在 Views->Home 文件夹中添加 2 个新视图,即 404.cshtml 和 500.cshtml

在 404.cshtml 里面你可以放一些类似的东西:

@{
    ViewBag.Title = "Page Not Found" ;    
}
<h1>(404) Page Not Found.</h1>
<p>This page doesn't exist.</p>
<p>Please, contact the support by emailing them this page error.</p>
<p>To find the available contacts go to: @Html.ActionLink("Contacts", "Contact", "Home")</p>

500.cshtml内部:

@{
    ViewBag.Title = "(500) An Error Occurred";
}
<h1>(500) An Error Occurred.</h1>
<p>Something went wrong.</p>
<p>Please, contact the support by emailing them this page error.</p>
<p>To find the available contacts go to: @Html.ActionLink("Contacts", "Contact", "Home")</p>

任何其他错误都会执行默认的 Error.cshtml 模板(我使用的是 Microsoft 默认的 Error.cshtml)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-02
    • 1970-01-01
    • 2020-08-22
    • 2020-08-26
    • 2020-01-03
    相关资源
    最近更新 更多