HttpContext.current.Session 在错误页面中被清除
我为我的 ASP.NET 4 应用程序制作了一个自定义错误页面。我将异常对象放在 HttpContext.current.Session["CustomError"]
中,但是当用户重定向到错误页面时 HttpContext.current.Session["CustomError"]
为空。 我在 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"] 包含错误对象,但第二次它为空。
I have made a custom error page for my ASP.NET 4 application. I put the exception object in HttpContext.current.Session["CustomError"]
but when the user is redirected to the error page HttpContext.current.Session["CustomError"]
is null.
I do it in CustomError class constructor like this:
public CustomError(enExceptionType ExceptionType) : base(ExceptionMessage(ExceptionType)) {
HttpContext.Current.Session["CustomError"] = this;
}
when I step over the code Session["Error"] contains the error object.
any idea?
UPDATE:
I removed custom error page from web.config and added this to glabal.asax:
void Application_Error(object sender, EventArgs e)
{
if (Context.IsCustomErrorEnabled)
{
Response.Redirect("~/Error.aspx");
}
}
by stepping through this function I noticed that when an exception is thrown this function is called two time, the first time Session["CustiomError"] contains the error object but the second time its null.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这解决了问题,但如果有人告诉我原因,我将不胜感激:)
This solved the problem, but I would appreciate it if someone tells me why :)
而不是使用
Response.redirect(URL)
(我假设您的代码中有它)使用
Server.Transfer(URL)
或
Response.redirect(url, false)
为什么是
Server.Transfer(url)
?来源此处。
请告诉我其中一项是否适合您。
更新:
如果您使用网络配置设置,您可以尝试将 ResponseWrite 值添加到重定向模式变量吗?
如果这仍然不起作用,我建议实施 这(我已经在我的应用程序中完成了将错误记录在日志文件中(对于我作为管理员)并向用户呈现一般错误)。
Instead of using
Response.redirect(URL)
(which I assume you have in your code) useServer.Transfer(URL)
or
Response.redirect(url, false)
Why
Server.Transfer(url)
?Source here.
Please let me know if one of these works for you.
UPDATE:
If you use a web config setting can you try adding ResponseWrite value to redirectmode var?
If this is still not working I suggest to implement this (I've done it in my application to log the errors in log files (for me as admin) and present a generic error to the user).