【发布时间】:2011-08-11 09:58:22
【问题描述】:
我为我的 ASP.NET 4 应用程序制作了一个自定义错误页面。我将异常对象放在HttpContext.current.Session["CustomError"] 中,但是当用户被重定向到错误页面时HttpContext.current.Session["CustomError"] 是null。
我在 CustomError 类构造函数中这样做:
public CustomError(enExceptionType ExceptionType) : base(ExceptionMessage(ExceptionType)) {
HttpContext.Current.Session["CustomError"] = this;
}
当我跳过代码时,Session["Error"] 包含错误对象。 有什么想法吗?
更新:
我从 web.config 中删除了自定义错误页面并将其添加到 glabal.asax:
void Application_Error(object sender, EventArgs e)
{
if (Context.IsCustomErrorEnabled)
{
Response.Redirect("~/Error.aspx");
}
}
通过单步执行此函数,我注意到当抛出异常时,此函数被调用两次,第一次 Session["CustiomError"] 包含错误对象,但第二次为 null。
【问题讨论】:
-
你想用自定义错误会话实现什么,如果你想记录它,你可以使用全局 asax 中的 application_error 事件
-
我想在错误页面显示自定义错误对象的错误信息。
-
你能把代码放在你分配异常的地方吗?
-
我更新了帖子并添加了代码
-
@nima 你确定在你填写异常值之后重定向就完成了吗?
标签: asp.net session-variables custom-error-pages