我正在尝试将 HTTP 403 错误代码返回给客户端。我读过 HttpException 是完成此任务的最干净的方法,但它对我不起作用。我从这样的页面中抛出异常:
throw new HttpException(403,"You must be logged in to access this resource.");
但是,这只会在 CustomErrors 关闭时提供标准的 ASP.Net 堆栈跟踪(带有 500 错误)。如果启用了 CustomErrors,那么这将不会重定向到我设置的页面,以便在发生 403 错误时显示。我应该忘记 HttpException 而是自己设置所有 HTTP 代码吗?我该如何解决?
我的 Web.Config 的自定义错误部分是这样的:
<customErrors mode="On" defaultRedirect="GenericErrorPage.html">
<error statusCode="403" redirect="Forbidden.html" />
</customErrors>
我不会得到 Forbidden.html,而是得到 GenericErrorPage.html