.net会话状态已创建会话ID,但无法保存,因为响应已被应用程序刷新

时间:2009-09-24 16:37:08

标签: c# global-asax

我的global.asax Session_Start

中有以下内容
string myBrowser = Utils.SafeUserAgent(Request);
foreach (string bannedbrowser in BrowserBan.BrowsersToBan)
{
   if (myBrowser.IndexOf(bannedbrowser, StringComparison.OrdinalIgnoreCase) > -1)
   {
       HttpContext.Current.Response.Redirect("/bannedBrowser.htm");
       Session.Abandon();
       break;
   }

它会阻止转码器访问我的网站。但我时不时会得到一个错误

System.Web.HttpException: Session state has created a session id, but cannot
  save it because the response was already flushed by the application.
    at System.Web.SessionState.SessionIDManager.SaveSessionID(
      HttpContext context, String id, Boolean& redirected, Boolean& cookieAdded)
    at System.Web.SessionState.SessionStateModule.CreateSessionId()
    at System.Web.SessionState.SessionStateModule.DelayedGetSessionId()
    at System.Web.SessionState.SessionStateModule.ReleaseStateGetSessionID()
    at System.Web.SessionState.SessionStateModule.OnReleaseState(Object source,
      EventArgs eventArgs)
    at System.Web.SessionState.SessionStateModule.OnEndRequest(Object source,
      EventArgs eventArgs)
    at System.Web.HttpApplication.SyncEventExecutionStep.System.Web
      .HttpApplication.IExecutionStep.Execute()
    at System.Web.HttpApplication.ExecuteStep(IExecutionStep step,
      Boolean& completedSynchronously)

只有在我尝试通过(例如)Google Transcoder访问它时才会发生这种情况,但我想了解它为什么会发生以及如何防止它。

我必须放弃会话,以便重新评估刷新用户代理。

2 个答案:

答案 0 :(得分:4)

你见过this thread吗?

我遇到了这个异常(在不同的上下文中)并使用以下内容修复它...

void Session_Start(object sender, EventArgs e) 
{
string id = Session.SessionID;
}

答案 1 :(得分:2)

您是否尝试过放弃会话与重定向的顺序?

Session.Abandon();
HttpContext.Current.Response.Redirect("/bannedBrowser.htm", true);