Razor 自定义错误页面忽略它的 OnGet 处理程序
Razor custom error page ignores it's OnGet handler
有自定义错误页面:
public sealed class ErrorModel : PageModel
{
public ErrorModel()
{
// app stops at breakpoint inside ctor
}
public IActionResult OnGet()
{
// app doesn't stop here, why?
// this is for debugging purposes only
return BadRequest();
}
}
及其观点:
@page
@model ErrorModel
@if (Model != null)
{
<p>
The error page.
</p>
}
页面以常规方式注册:
app.UseExceptionHandler("/Error");
当应用程序重定向到错误页面时,它会在页面构造函数和页面视图中的断点处停止,但会忽略 OnGet
中的代码。由于应用程序不调用我的 OnGet
,页面呈现时没有我在 OnGet
.
中填写的任何有用数据
已启用所有断点 - 我正在调试实际代码。
知道发生了什么以及如何解决这个问题吗?
UPD
我减少了页面模型代码和视图。
当应用停在这一行时:
@if (Model != null)
调用堆栈是:
Portal.App.Views.dll!Portal.Pages.Pages_Error.ExecuteAsync() Line 4 C#
Microsoft.AspNetCore.Mvc.RazorPages.dll!Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.RazorPageAdapter.ExecuteAsync() Unknown
Microsoft.AspNetCore.Mvc.Razor.dll!Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderPageCoreAsync(Microsoft.AspNetCore.Mvc.Razor.IRazorPage page, Microsoft.AspNetCore.Mvc.Rendering.ViewContext context) Unknown
Microsoft.AspNetCore.Mvc.Razor.dll!Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderPageAsync(Microsoft.AspNetCore.Mvc.Razor.IRazorPage page, Microsoft.AspNetCore.Mvc.Rendering.ViewContext context, bool invokeViewStarts) Unknown
Microsoft.AspNetCore.Mvc.Razor.dll!Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderAsync(Microsoft.AspNetCore.Mvc.Rendering.ViewContext context) Unknown
Microsoft.AspNetCore.Mvc.ViewFeatures.dll!Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor.ExecuteAsync(Microsoft.AspNetCore.Mvc.Rendering.ViewContext viewContext, string contentType, int? statusCode) Unknown
Microsoft.AspNetCore.Mvc.RazorPages.dll!Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageResultExecutor.ExecuteAsync(Microsoft.AspNetCore.Mvc.RazorPages.PageContext pageContext, Microsoft.AspNetCore.Mvc.RazorPages.PageResult result) Unknown
Microsoft.AspNetCore.Mvc.RazorPages.dll!Microsoft.AspNetCore.Mvc.RazorPages.PageResult.ExecuteResultAsync(Microsoft.AspNetCore.Mvc.ActionContext context) Unknown
因此,应用程序执行 PageResult
。这个结果从何而来?
UPD 2
这看起来像是身份验证问题。当用户通过身份验证时,一切都按预期工作。但对于未经身份验证的用户,我的 OnGet
不会被调用。 ErrorModel
没有 Authorize
属性。身份验证对此有何影响?
我没有任何自定义身份验证配置,因此,我假设使用默认设置:
private void ConfigureMvcServices(IServiceCollection services)
{
services
.AddMvc()
// another services;
}
错误页面位于 Pages
文件夹中 - Pages/Error
。
另外,如果用户没有认证,而页面需要认证,一定会返回401,不是吗?
您是否在 Web 配置中启用了自定义错误。像这样:
<httpErrors errorMode="Custom" existingResponse="Replace">
<clear />
<error statusCode="500" path="/Error" responseMode="ExecuteURL" />
</httpErrors>
我已经弄明白了。陷阱是here:
To configure a custom error handling page for the Production
environment, use the Exception Handling Middleware. The middleware:
- Catches and logs exceptions.
- Re-executes the request in an alternate pipeline for the page or controller indicated. The request isn't re-executed if the response
has started.
"Re-executes" 的意思是,如果在 GET 处理程序中引发异常,中间件将调用错误页面的 OnGet。如果在 POST 处理程序中引发,中间件调用 OnPost 等:
public class ErrorModel : PageModel
{
public ErrorModel()
{
}
public void OnGet()
{
// This executes when exception was in OnGet
}
public void OnPost()
{
// This executes when exception was in OnPost
}
// ...
}
在我的例子中,在经过身份验证的页面上有一个 GET 请求,而 POST 在未经身份验证的页面上有一个请求,所以这是行为上的差异。
有自定义错误页面:
public sealed class ErrorModel : PageModel
{
public ErrorModel()
{
// app stops at breakpoint inside ctor
}
public IActionResult OnGet()
{
// app doesn't stop here, why?
// this is for debugging purposes only
return BadRequest();
}
}
及其观点:
@page
@model ErrorModel
@if (Model != null)
{
<p>
The error page.
</p>
}
页面以常规方式注册:
app.UseExceptionHandler("/Error");
当应用程序重定向到错误页面时,它会在页面构造函数和页面视图中的断点处停止,但会忽略 OnGet
中的代码。由于应用程序不调用我的 OnGet
,页面呈现时没有我在 OnGet
.
已启用所有断点 - 我正在调试实际代码。
知道发生了什么以及如何解决这个问题吗?
UPD
我减少了页面模型代码和视图。 当应用停在这一行时:
@if (Model != null)
调用堆栈是:
Portal.App.Views.dll!Portal.Pages.Pages_Error.ExecuteAsync() Line 4 C# Microsoft.AspNetCore.Mvc.RazorPages.dll!Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.RazorPageAdapter.ExecuteAsync() Unknown Microsoft.AspNetCore.Mvc.Razor.dll!Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderPageCoreAsync(Microsoft.AspNetCore.Mvc.Razor.IRazorPage page, Microsoft.AspNetCore.Mvc.Rendering.ViewContext context) Unknown Microsoft.AspNetCore.Mvc.Razor.dll!Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderPageAsync(Microsoft.AspNetCore.Mvc.Razor.IRazorPage page, Microsoft.AspNetCore.Mvc.Rendering.ViewContext context, bool invokeViewStarts) Unknown Microsoft.AspNetCore.Mvc.Razor.dll!Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderAsync(Microsoft.AspNetCore.Mvc.Rendering.ViewContext context) Unknown Microsoft.AspNetCore.Mvc.ViewFeatures.dll!Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor.ExecuteAsync(Microsoft.AspNetCore.Mvc.Rendering.ViewContext viewContext, string contentType, int? statusCode) Unknown Microsoft.AspNetCore.Mvc.RazorPages.dll!Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageResultExecutor.ExecuteAsync(Microsoft.AspNetCore.Mvc.RazorPages.PageContext pageContext, Microsoft.AspNetCore.Mvc.RazorPages.PageResult result) Unknown Microsoft.AspNetCore.Mvc.RazorPages.dll!Microsoft.AspNetCore.Mvc.RazorPages.PageResult.ExecuteResultAsync(Microsoft.AspNetCore.Mvc.ActionContext context) Unknown
因此,应用程序执行 PageResult
。这个结果从何而来?
UPD 2
这看起来像是身份验证问题。当用户通过身份验证时,一切都按预期工作。但对于未经身份验证的用户,我的 OnGet
不会被调用。 ErrorModel
没有 Authorize
属性。身份验证对此有何影响?
我没有任何自定义身份验证配置,因此,我假设使用默认设置:
private void ConfigureMvcServices(IServiceCollection services)
{
services
.AddMvc()
// another services;
}
错误页面位于 Pages
文件夹中 - Pages/Error
。
另外,如果用户没有认证,而页面需要认证,一定会返回401,不是吗?
您是否在 Web 配置中启用了自定义错误。像这样:
<httpErrors errorMode="Custom" existingResponse="Replace">
<clear />
<error statusCode="500" path="/Error" responseMode="ExecuteURL" />
</httpErrors>
我已经弄明白了。陷阱是here:
To configure a custom error handling page for the Production environment, use the Exception Handling Middleware. The middleware:
- Catches and logs exceptions.
- Re-executes the request in an alternate pipeline for the page or controller indicated. The request isn't re-executed if the response has started.
"Re-executes" 的意思是,如果在 GET 处理程序中引发异常,中间件将调用错误页面的 OnGet。如果在 POST 处理程序中引发,中间件调用 OnPost 等:
public class ErrorModel : PageModel
{
public ErrorModel()
{
}
public void OnGet()
{
// This executes when exception was in OnGet
}
public void OnPost()
{
// This executes when exception was in OnPost
}
// ...
}
在我的例子中,在经过身份验证的页面上有一个 GET 请求,而 POST 在未经身份验证的页面上有一个请求,所以这是行为上的差异。