UseStatusCodePagesWithReExecute 执行原始请求两次,对于 NotFound 结果没有错误响应

Mar*_*ark 5 asp.net-core razor-pages

UseStatusCodePagesWithReExecute我在尝试为返回 NotFound 结果的页面请求显示自定义 404 错误页面时,在 dotnet razor Pages Web 应用程序中遇到了问题。具体来说,如果我从 OnGet 方法返回 NotFound,我会发现再次调用相同的请求,并且永远不会重新执行提供给中间件的路径。

我正在使用 .NET Core 3.0,因此尚未尝试过以前的版本或 3.1 预览版。

我已经成功地通过一个简单的重现来复制了这个问题。以下将允许无效路由重定向到错误页面(例如https://localhost:5001/foo),但是,该路由https://localhost:5001/将被调用两次并且不会重定向到错误页面。

所以我要问的问题是,这是一个错误还是我在这里遗漏了一些概念?我已经尝试过相关的UseStatusCodePagesWithRedirects方法,并且它执行了它所说的操作,但如果可以的话,我真的很想使用重新执行。

再现

环境:

  • Windows 10
  • 点网核心3.0
  • chrome 和 Edge 浏览器(未尝试过其他浏览器)- 无论如何都不认为这是浏览器问题,因为没有重新提交。

脚步:

  1. 创建一个模板 razor 项目dotnet new webapp -n myapp
  2. 编辑Index.cshtml.cs OnGet方法如下:
        public IActionResult OnGet()
        {
            return NotFound();
        }
Run Code Online (Sandbox Code Playgroud)
  1. 编辑Startup.cs并添加app.UseStatusCodePagesWithReExecute("/Error");到配置方法中的 if/else 代码块后面,如下所示:
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/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();
            }

            // Added this line
            app.UseStatusCodePagesWithReExecute("/Error");

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

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapRazorPages();
            });
        }
Run Code Online (Sandbox Code Playgroud)
  1. 在第一行的 OnGet 中放置一个断点(假设是 VSCode 或 Visual Studio 2019)。
  2. 在对索引页的第一个请求之后运行调试并按 F5。不会发生重定向/Error,而是再次命中断点。
  3. 再次按 F5,浏览器将显示标准 404,而不是错误页面。

提前致谢。

Dev*_*SFT 3

StatusCodePages 中间件有 3 种方法来显示自定义错误页面,它们是 UseStatusCodePagesUseStatusCodePagesWithReditectsUseStatusCodePagesWithReExecute。如果您想通过编辑方法NotFound每次访问https://localhost:5001/Index.cshtml.cs OnGet时都返回结果,您应该使用UseStatusCodePages自定义 404 错误页面而不是UseStatusCodePagesWithReExecute.

不过,如果您坚持使用UseStatusCodePagesWithReExecute,则不需要返回真正的NotFound. UseStatusCodePagesWithReExecute用于不存在的资源。一旦你访问 https://localhost:5001/any-non-existing-page,它会抛出 404,然后重新执行你自己的错误页面的请求。但这并不是 NotFound方法的替代。同时,使用时要注意一些条件和限制UseStatusCodePagesWithReExecute,可以参考这篇文章:https://learn.microsoft.com/en-us/aspnet/core/fundamentals/error-handling ?view=aspnetcore- 3.1 .

这是一个工作示例。

  1. 编辑 Startup.cs 配置方法,如下所示:

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
        //app.UseDeveloperExceptionPage();
        }
        else
        {
       // app.UseExceptionHandler("/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.UseStatusCodePages();
        app.UseStatusCodePagesWithReExecute("/Error", "?code={0}");
       // app.UseHttpsRedirection();
    
        app.UseStaticFiles();
    
        //app.UseStatusCodePagesWithRedirects("/Error");
    
        app.UseRouting();
    
        app.UseAuthorization();
    
        app.UseEndpoints(endpoints =>
        {
            endpoints.MapRazorPages();
        });
    
    }
    
    Run Code Online (Sandbox Code Playgroud)
  2. 将@page“{code?}”添加到Error.cshtml:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    if (env.IsDevelopment())
    {
    //app.UseDeveloperExceptionPage();
    }
    else
    {
   // app.UseExceptionHandler("/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.UseStatusCodePages();
    app.UseStatusCodePagesWithReExecute("/Error", "?code={0}");
   // app.UseHttpsRedirection();

    app.UseStaticFiles();

    //app.UseStatusCodePagesWithRedirects("/Error");

    app.UseRouting();

    app.UseAuthorization();

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

}
Run Code Online (Sandbox Code Playgroud)

  1. 当访问https://localhost:52602/hhh时,这是 Edge 中不存在的页面,浏览器显示此页面:

在此输入图像描述