c# - 如何在不使用 web.config 的情况下从后面的代码中检测错误以创建自定义错误页面?
问题描述
我正在尝试使用 asp.net 中的代码而不是 web.config 检测错误 404 并显示自定义错误页面。该代码不起作用,它只是提供默认错误页面。这是我的代码:
Exception exception = Server.GetLastError();
HttpException httpException = (HttpException)exception;
Server.ClearError();
if (httpException.ErrorCode == 404)
{
Response.Redirect("Page_Not_Found.aspx");
}
解决方案
更好的方法是通过web.confng
如下方式处理它。
<customErrors mode="On" defaultRedirect="~/UnexpectedError.html">
<error redirect="~/404Error.html" statusCode="404" />
</customErrors>
推荐阅读
- javascript - 如何在视口顶部应用滚动动画+计算适当的百分比?
- encoding - dotNetRDF owl 文件编码为中文
- python - ImportError:无法从“jupyter_client.provisioning”导入名称“KernelProvisionerBase”
- html - 我将样式 css 文件设置为设计,但它不显示为原始页面?
- azure - Terraform Azure 服务主体客户端密码过期
- c# - 为什么这个类层次结构违反 LSP?
- javascript - 根据域白名单设置 cookie 策略
- microsoft-graph-api - 如何获取消息的 immutalbeId
- sql - SQL Server:在选择语句中按不同条件转换列
- c++ - 在没有 XCHG 的情况下实现自旋锁?