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方法,并且它执行了它所说的操作,但如果可以的话,我真的很想使用重新执行。
再现
环境:
脚步:
dotnet new webapp -n myapp。Index.cshtml.cs OnGet方法如下: public IActionResult OnGet()
{
return NotFound();
}
Run Code Online (Sandbox Code Playgroud)
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)
/Error,而是再次命中断点。提前致谢。
StatusCodePages 中间件有 3 种方法来显示自定义错误页面,它们是
UseStatusCodePages、UseStatusCodePagesWithReditects和UseStatusCodePagesWithReExecute。如果您想通过编辑方法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 .
这是一个工作示例。
编辑 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)将@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)
| 归档时间: |
|
| 查看次数: |
2383 次 |
| 最近记录: |