无限循环中的自定义 404 页面重定向循环

Custom 404 page redirect loops in a infinite loop

我的问题是当我访问我的网站时。com/adsadasdsad 我被重定向到 /Error/404 大约 20 次,直到浏览器放弃。 我的代码是:

Startup.cs

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace _projectname
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            //Register dependencies
            services.AddRazorPages();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.Use(async (ctx, next) =>
                {
                    await next();

                    if (ctx.Response.StatusCode == 404 && !ctx.Response.HasStarted)
                    {
                        //Re-execute the request so the user gets the error page
                        string originalPath = ctx.Request.Path.Value;
                        ctx.Items["originalPath"] = originalPath;
                        ctx.Request.Path = "/Error";
                        await next();
                    }
                });
                // orig
                System.Diagnostics.Debug.WriteLine("bah");
                //app.UseExceptionHandler("/Error/{0}");
                app.UseStatusCodePagesWithRedirects("/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapRazorPages();
            });
        }
    }
}

在上面的代码中,我尝试交替使用 app.UseExceptionHandler 和 app.UseStatusCodePagesWithRedirects,但没有结果。

我的错误控制器:

using Microsoft.AspNetCore.Mvc;

namespace _projectname.Error;

public class ErrorController : Controller
{
    [Route("/Error/{statusCode}")]
    public IActionResult HttpStatusCodeHandler(int statusCode)
    {
        switch (statusCode)
        {
            case 404:
                System.Diagnostics.Debug.WriteLine("bah");
                ViewBag.ErrorMessage("Resource could not be found.");
                break;
            case 500:
                break;
        }
        return View("Error");
    }
}

我不明白我做错了什么。即使我更改了重定向 URL,重定向似乎也会循环,而且断点在我的 Visual Studio 2022 预览版中似乎不起作用。似乎也没有输出。

我的launchSettings.json如下:

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:28725",
      "sslPort": 44342
    }
  },
  "profiles": {
    "_34digital.ruhr": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "applicationUrl": "https://localhost:5001;http://localhost:5000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Production"
      }
    },
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Production"
      }
    }
  }
}

P.S。是的,我确实有一个共享 Error.cshtml 和一个名为“Error”的文件夹,其中包含 404.cshtml 和 500.cshtml

错误是错误文件夹不在 Pages 文件夹中 (facepalm)