asp.net 定制简单的错误处理页面
通常web应用程序在发布后,为了给用户一个友好界面和使用体验,都会在错误发生时跳转至一个自定义的错误页面,而不是asp.net向用户暴露出来的详细的异常列表。
简单的错误处理页面可以通过web.config来设置
<customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
<error statusCode="403" redirect="NoAccess.htm" />
<error statusCode="404" redirect="FileNotFound.htm" />
</customErrors>
void Application_Error(object sender, EventArgs e)
{
Exception objErr = Server.GetLastError().GetBaseException();
string error = "发生异常页: " + Request.Url.ToString() + "<br>";
error += "异常信息: " + objErr.Message + "<br>";
Server.ClearError();
Application["error"] = error;
Response.Redirect("~/ErrorPage/ErrorPage.aspx");
}
protected void Page_Load(object sender, EventArgs e)
{
ErrorMessageLabel.Text = Application["error"].ToString();
}
当最终用户使用应用程序的时候,他们可能不想知道错误的原因,这个时候,我们可以通过复选框来实现,是否呈现错误的原因。可将Label放在一个div中,然后用复选框来决定是否呈现div
<script language="javascript" type="text/javascript">
<!--

function CheckError_onclick() {
var chk = document.getElementById("CheckError");
var divError = document.getElementById("errorMsg");
if(chk.checked)
{
divError.style.display = "inline";
}
else
{
divError.style.display = "none";
}
}

// -->
</script>

我们可以对errorpage这页做一些更亲切的设计,让人看起来更舒服些。
简单的错误处理页面可以通过web.config来设置




如果想通过编程的方式来呈现错误原因,可以通过Page_Error事件来做这件事.
另一种方式则可以通过Global.asax来实现,我觉得这种方式较为方便,另外如果能结合一个单独的更加友好的页面,则看来起更舒服一些









ErrorPage.aspx






















我们可以对errorpage这页做一些更亲切的设计,让人看起来更舒服些。
KidYang
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· Blazor Hybrid适配到HarmonyOS系统
· Obsidian + DeepSeek:免费 AI 助力你的知识管理,让你的笔记飞起来!
· 解决跨域问题的这6种方案,真香!
· 一套基于 Material Design 规范实现的 Blazor 和 Razor 通用组件库
· 数据并发安全校验处理工具类