HttpContext.current.Session 在错误页面中被清除

发布于 2024-11-03 00:17:08 字数 796 浏览 8 评论 0原文

我为我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

芯好空 2024-11-10 00:17:08

这解决了问题,但如果有人告诉我原因,我将不胜感激:)

void Application_Error(object sender, EventArgs e)
{
    if (Context.IsCustomErrorEnabled)
    {
        Response.Redirect("~/Error.aspx");
        **Server.ClearError();**
    }
}

This solved the problem, but I would appreciate it if someone tells me why :)

void Application_Error(object sender, EventArgs e)
{
    if (Context.IsCustomErrorEnabled)
    {
        Response.Redirect("~/Error.aspx");
        **Server.ClearError();**
    }
}
白龙吟 2024-11-10 00:17:08

而不是使用 Response.redirect(URL) (我假设您的代码中有它)

使用Server.Transfer(URL)

Response.redirect(url, false)

为什么是Server.Transfer(url)

使用转移到另一个页面
Server.Transfer 节省服务器
资源。而不是告诉
浏览器重定向,它只是改变
Web 服务器上的“焦点”以及
传输请求。这意味着你
没有收到那么多的 HTTP 请求
通过,因此缓解
Web 服务器上的压力以及
使您的应用程序运行得更快。

来源此处

请告诉我其中一项是否适合您。

更新

如果您使用网络配置设置,您可以尝试将 ResponseWrite 值添加到重定向模式变量吗?

<customErrors mode="RemoteOnly" defaultRedirect="~/errors/GeneralError.aspx" redirectMode="ResponseRewrite" />

如果这仍然不起作用,我建议实施 (我已经在我的应用程序中完成了将错误记录在日志文件中(对于我作为管理员)并向用户呈现一般错误)。

Instead of using Response.redirect(URL) (which I assume you have in your code) use

Server.Transfer(URL)

or

Response.redirect(url, false)

Why Server.Transfer(url)?

Transferring to another page using
Server.Transfer conserves server
resources. Instead of telling the
browser to redirect, it simply changes
the "focus" on the Web server and
transfers the request. This means you
don't get quite as many HTTP requests
coming through, which therefore eases
the pressure on your Web server and
makes your applications run faster.

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?

<customErrors mode="RemoteOnly" defaultRedirect="~/errors/GeneralError.aspx" redirectMode="ResponseRewrite" />

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文