Session_Start中的Response.Redirect不起作用 [英] Response.Redirect in Session_Start doesn't work

查看:94
本文介绍了Session_Start中的Response.Redirect不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的 Session_Start 代码,如下所示:

I have a simple Session_Start code that looks like this:

Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
    Dim sid = Session.SessionID
    Response.Redirect("~/Blog.aspx")
    dim dummy=4/0
End Sub

它不能按预期方式工作.通常在我的整个站点中,每当调用 Response.Redirect()时,它也会终止代码执行.而在这里,即使页面最终重定向,也会执行 dim dummy = 4/0 行.

It does not work as expected. Usually in my whole site, whenever Response.Redirect() is called, it also terminates the code execution. Whereas here, even if the page eventually redirects, the dim dummy=4/0 line is also executed.

这使我从 Session_Start()调用的其他代码中遇到了问题,在该代码中,我基于重定向是出口点的假设进行构建.

This is causing me problems in other code called from Session_Start() where I built on the assumption that the redirect is an exit point.

我还尝试将 Response.Redirect(url,endResponse)重载方法中的 endResponse 设置为 true false ,但这也不起作用.

I also tried setting endResponse in the Response.Redirect(url, endResponse) overloaded method as true or false but this doesn't work either.

推荐答案

深入研究框架源代码,我可以解释为什么 Response.Redirect(url,true)在之后继续执行代码在 Session_Start()中被调用,但在后面的常规代码中不会被调用.

Having had a dig into the framework source code I can explain why Response.Redirect(url, true) continues to execute code after being called in Session_Start() but not in regular code behind.

Response.Redirect()最终调用 Redirect()的内部重载方法:

Response.Redirect() ultimately calls an internal overloaded method of Redirect():

internal void Redirect(string url, bool endResponse, bool permanent)
{
  // Miscellaneous goings on

  if (endResponse)
  {
    this.End();
  }
}

在此方法的结尾,如果 endResponse 为true,则将调用 Response.End().当我们查看 Response.End()时,我们看到以下代码:

At the end of this method, if endResponse is true then Response.End() is called. When we look at Response.End() we see the following code:

public void End()
{
    if (this._context.IsInCancellablePeriod)
    {
        InternalSecurityPermissions.ControlThread.Assert();
        Thread.CurrentThread.Abort(new HttpApplication.CancelModuleException(false));
    }
    else if (!this._flushing)
    {
        this.Flush();
        this._ended = true;
        if (this._context.ApplicationInstance != null)
        {
            this._context.ApplicationInstance.CompleteRequest();
        }
    }
}

该方法检查当前上下文的 IsInCancellablePeriod 值的状态.这个值是内部的,但是我们可以在调试器中看到它:

The method examines the state of the current context's IsInCancellablePeriod value. This value is internal but we can see it in our debugger:

如果我们在 Session_Start()内设置一个断点并检查当前上下文的 IsInCancellablePeriod 不可见成员,我们将看到:

If we set a breakpoint inside Session_Start() and examine the current context's IsInCancellablePeriod non-visible member we see:

这意味着不会中断请求的线程,因此无论您是否设置了 endResponse Response.Redirect()之后的代码都将执行.

This means that the request's thread won't be aborted and so the code after Response.Redirect() will execute, regardless of whether you set endResponse or not.

如果我们在ASPX页面的 Page_Load()事件中设置断点,则会看到不同的内容:

If we set a breakpoint inside an ASPX page's Page_Load() event we see something different:

当前上下文的 IsInCancellablePeriod 不可见成员设置为true,因此将调用 Thread.CurrentThread.Abort(),并且 Response.Redirect().

The current context's IsInCancellablePeriod non-visible member is set to true and so Thread.CurrentThread.Abort() will be called and no more code will execute after the Response.Redirect().

这种行为差异的原因是我怀疑与保护您的会话状态完整性有关:

The reason for this difference in behaviour is I suspect to do with protecting your session state integrity:

如果需要阻止在 Session_Start()中的 Response.Redirect()之后执行代码,则需要使用 If.然后...其他:

If you need to prevent code from executing after a Response.Redirect() in Session_Start() then you'll need to use an If...Then...Else:

If <some_condition_we_have_to_redirect_for> Then
    Response.Redirect("~/Blog.aspx")
Else
    // Normal session start code goes here
End If

这篇关于Session_Start中的Response.Redirect不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆