【问题标题】:UseStatusCodePagesWithReExecute executes original request twice without error response for NotFound resultsUseStatusCodePagesWithReExecute 对 NotFound 结果执行两次原始请求而没有错误响应
【发布时间】:2020-03-27 07:26:44
【问题描述】:

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

我使用的是 .NET Core 3.0,所以没有尝试过以前的版本或 3.1 预览版。

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

所以我要问的问题是,这是一个错误还是我在这里遗漏了一些概念?我已经尝试过相关的 UseStatusCodePagesWithRedirects 方法,它确实按照它说的去做,但如果可以的话,我真的很想使用 ReExecute。

复制

环境:

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

步骤:

  1. 创建模板剃须刀项目dotnet new webapp -n myapp
  2. 编辑Index.cshtml.csOnGet方法如下:
        public IActionResult OnGet()
        {
            return NotFound();
        }
  1. 编辑 Startup.cs 并在 Configure 方法中的 if/else 代码块之后添加 app.UseStatusCodePagesWithReExecute("/Error");,如下所示:
        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();
            });
        }
  1. 在 OnGet 的第一行放置一个断点(假设是 VSCode 或 Visual Studio 2019)。
  2. 在第一个请求之后运行调试和 F5 到索引页面。不会重定向到 /Error,而是再次命中断点。
  3. 再次按 F5,浏览器会显示其标准 404 而不是错误页面。

提前致谢。

【问题讨论】:

    标签: asp.net-core razor-pages


    【解决方案1】:

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

    不过,如果您坚持使用UseStatusCodePagesWithReExecute,则无需返回真正的NotFoundUseStatusCodePagesWithReExecute 用于不存在的资源。一旦你访问https://localhost:5001/any-non-exisisting-page,它会抛出404,然后重新执行你自己的错误页面的请求。但它不能替代NotFound 方法。同时,使用UseStatusCodePagesWithReExecute时要注意一些条件和限制,可以参考这篇文章:https://docs.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();
          });
      
      }
      
    2. 将@page“{code?}”添加到Error.cshtml:

    @page "{code?}"
    @model ErrorModel
    @{
        ViewData["Title"] = "Error";
    }
    
    <h1 class="text-danger">Error.</h1>
    <h1 class="text-danger">Status Code: @Model.ErrorStatusCode</h1>
    
    <h2 class="text-danger">An error occurred while processing your request.</h2>
    
    @if (Model.ShowRequestId)
    {
        <p>
            <strong>Request ID:</strong> <code>@Model.RequestId</code>
        </p>
    }
    
    <h3>Development Mode</h3>
    <p>
        Swapping to the <strong>Development</strong> environment displays detailed information about the error that occurred.
    </p>
    <p>
        <strong>The Development environment shouldn't be enabled for deployed applications.</strong>
        It can result in displaying sensitive information from exceptions to end users.
        For local debugging, enable the <strong>Development</strong> environment by setting the <strong>ASPNETCORE_ENVIRONMENT</strong> environment variable to <strong>Development</strong>
        and restarting the app.
    </p>
    1. 当访问https://localhost:52602/hhh是Edge中不存在的页面时,浏览器会显示这个页面:

    【讨论】:

      【解决方案2】:

      如果你使用

      return new NotFoundResult();
      

      而不是

      return NotFound();
      

      它将使用所需的管道,唯一的缺点是无法传递消息,如果要传递消息,则需要构建自己的中间件来处理这种情况。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-07-30
        • 1970-01-01
        • 2014-09-19
        • 2021-10-12
        • 2021-06-01
        相关资源
        最近更新 更多