【问题标题】:Session is null after ResponseRewriteResponseRewrite 后会话为空
【发布时间】:2013-04-30 03:10:18
【问题描述】:

我正在使用 web.config 中的自定义错误属性来处理自定义错误

像这样:

<customErrors mode="On" defaultRedirect="~/Error.aspx" redirectMode="ResponseRewrite" />

抛出错误后,页面被重定向到错误页面,但是当我访问错误页面中的会话时,它是空的。

我使用 ResponseRewrite 而不是 ResponseRedirect 的原因是因为我使用 elmah 将 Exception id 通过 Items。

我什至尝试创建新的空 asp.net 网站,但它仍然发生。

我见过一些类似的问题,但没有答案。

【问题讨论】:

  • 您能添加代码或更多详细信息吗?我没有重现 Visual Studio 2012 IIS 8 中的错误。
  • 我正在使用 Visual Studio 2010, iis6

标签: c# asp.net web-config elmah


【解决方案1】:

我重现了这个问题,但我不明白为什么会发生这种情况。在Application_Error 处理程序中,我可以访问 Session 变量,但是当页面加载时,它变为null

我找到了解决问题的解决方法here。您需要从 web.config 中删除 redirectMode 并在出现错误时手动执行 Server.Transfer。这就是 web.config:

<customErrors mode="On" defaultRedirect="~/Error.aspx"/>

并将其添加到Global.asax.cs 文件中:

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

要根据错误指定不同的错误页面,您可以像这样访问错误代码:

HttpException httpException = (HttpException) Server.GetLastError();
int httpCode = httpException.GetHttpCode();
switch (httpCode)
{
  case 500: Server.Transfer("~/Pages/Error.aspx");break;
  case 404: Server.Transfer("~/Pages/PageNotFound.aspx");break;
  default: Server.Transfer("~/Pages/Error.aspx");break;    
}

【讨论】:

  • 好的,我确实设法使用您的建议使其工作,但是您知道如何根据我所在的页面从网络配置中获取默认重定向吗?
  • @Amir 您想对每个页面使用不同的重定向,还是只想知道如何从 web.config 读取 defaultRedirect 属性?
  • 我知道如何读取默认重定向,但是当我想使用不同的重定向时会发生什么?
  • @Amir 我用一些不同重定向的信息更新了我的帖子
猜你喜欢
  • 2010-12-08
  • 2015-02-14
  • 2021-02-22
  • 2016-06-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-26
相关资源
最近更新 更多