C# .net会话状态已创建会话id,但无法保存,因为应用程序已刷新响应
我的global.asaxC# .net会话状态已创建会话id,但无法保存,因为应用程序已刷新响应,c#,global-asax,C#,Global Asax,我的global.asaxSession\u Start string myBrowser = Utils.SafeUserAgent(Request); foreach (string bannedbrowser in BrowserBan.BrowsersToBan) { if (myBrowser.IndexOf(bannedbrowser, StringComparison.OrdinalIgnoreCase) > -1) { HttpContext.Cu
Session\u 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)
这只发生在我试图通过(例如)谷歌转码器访问它时,但我想了解它为什么会发生,以及如何防止它
我必须放弃会话,以便在刷新时重新评估用户代理。您是否尝试过颠倒放弃会话与重定向的顺序
Session.Abandon();
HttpContext.Current.Response.Redirect("/bannedBrowser.htm", true);
你看到了吗
我遇到了这个异常(在另一个上下文中),并用以下内容修复了它
void Session_Start(object sender, EventArgs e)
{
string id = Session.SessionID;
}
将sessionid分配到字符串将如何帮助我?我不需要会话id,一旦我知道它是一个转码器。我不想在刷新会话id后分配它,因为我需要检查下一个请求是否是转码器。据推测,
SessionID
get访问器有一些副作用行为,导致在访问会话id时初始化会话id。Session\u Start
事件在页面生命周期中可能足够早,因此会话ID在此后的某个时间点安全保存,并且不会在OP中产生错误。因此,这一点与获取会话ID字符串值无关,但是关于简单地让SessionID
get访问器运行,我已经做了,但是需要时间才能看到结果,因为我没有在每个转码器上都发现错误request@JustLoren-有完全相同的问题,并且您的解决方案解决了它;-)谢谢。顺便说一下:在大多数情况下,你似乎在做的事情(阻止某些浏览器访问某个页面)会被认为是不好的做法。您可能依赖于客户端发送的用户代理字符串,这很容易被欺骗。如果您只是想警告用户潜在的不兼容性问题,这是合理的。但从您使用的语言(“禁用浏览器”)来看,我认为这不仅仅是一个不兼容警告。当然,你可能已经知道这一点,并且有你这样做的理由。这是一个移动网站,有时谷歌搜索的用户会通过谷歌的网络转码器来访问它,这会删除所有内容并扼杀用户体验。有时,人们/会员使用gogole代码转换器作为代理。答案如下: