ASP.NET Core 3.1 改变视图而不是重定向

ASP.NET Core 3.1 change view rather than redirect

目前,当用户没有特定页面的权限(即403)时,它将重定向到Account/AccessDenied页面。

当用户未经授权时,是否有任何方法可以更改请求的端点视图而不是重定向到另一个端点?

例如,当用户被授权时,he/she会得到这样请求的页面:

URL: https://example.com/secret
-------------------------------------------------
SiteName Page1 Page2
-------------------------------------------------

The secret page content.

-------------------------------------------------
About Contact Support

并且当用户未授权时,he/she会得到这样的东西:

URL: https://example.com/secret
-------------------------------------------------
SiteName Page1 Page2
-------------------------------------------------

403
Access Denied
You do not have permission to access this page.

-------------------------------------------------
About Contact Support

要根据状态代码显示自定义视图,我们可以使用 UseStatusCodePagesWithReExecute 中间件。

更多详细信息,请参阅:Re-execute the middleware pipeline with the StatusCodePages Middleware to create custom error pages