简体   繁体   English

Response.Redirect() 问题

[英]Response.Redirect() problems

Throughout my program, I always used Response.Redirect() method.在我的整个程序中,我总是使用Response.Redirect()方法。 I always assumed that whenever this method is called, the current page is terminated and the redirected page is initiated.我一直认为,每当调用此方法时,当前页面都会终止并启动重定向页面。 But then, i realized that Response.Redirect() has a second parameter: bool endResponse .但是后来,我意识到 Response.Redirect() 有第二个参数: bool endResponse So I concluded that the page doesnt get terminated...(Correct me if I'm wrong)所以我得出的结论是页面没有被终止......(如果我错了,请纠正我)

But at some point I needed to use Response.AddHeader("REFRESH","3;URL=mypage.aspx") to delay the page redirection.但在某些时候,我需要使用Response.AddHeader("REFRESH","3;URL=mypage.aspx")来延迟页面重定向。 But now I'm afraid the page doesn't get terminated.但现在恐怕页面不会被终止。 So I decided to use Page.Unload event.所以我决定使用Page.Unload事件。 But I don't know how to use it.但我不知道如何使用它。

Anyone can explain to me how to properly end the life cycle of a page?任何人都可以向我解释如何正确结束页面的生命周期?

The page in both cases really much gets terminated.在这两种情况下,页面实际上都被终止了。

The "default" of that 2nd value (true - default) means that the code will TERMINATE!第二个值的“默认”(真 - 默认)意味着代码将终止!

If you set the value = false, then the re-direct occurs, but any code you have that follows will keep running.如果您设置 value = false,则会发生重定向,但您拥有的任何代码都将继续运行。

Thus:因此:

    Response.Redirect("WebForm2.aspx")
    Debug.Print("this code will NOT run")

    or

    Response.Redirect("WebForm2.aspx", True)    ' true is default
    Debug.Print("this code will NOT run")

    but:

    Response.Redirect("WebForm2.aspx", False) 
    Debug.Print("this code WILL RUN - so will rest of code")

So, setting the value to true as you started or discovered?那么,在您开始或发现时将值设置为 true 吗? It will not change one thing, since that is/was the default in the first place.它不会改变一件事,因为那是/首先是默认设置。

So, if you use "false", then the code in that routine will continue to run until the sub exit (end sub) and then the other page will run along with its normal event cycle.因此,如果您使用“false”,则该例程中的代码将继续运行,直到子退出(结束子),然后另一个页面将与其正常事件循环一起运行。

So, the true/false really just determines if you want a hard exit.所以,真/假真的只是决定你是否想要一个硬退出。

And if you have a few more lines of code that you need or want to run AFTER the response.redirect?如果您需要或想要在 response.redirect 之后运行更多代码行? Well, then one could:那么,可以:

Move the response.Redirect AFTER those lines of code, or as noted, use "false" to not terminate the execution of the code in the current sub.在这些代码行之后移动 response.Redirect,或者如前所述,使用“false”来不终止当前子代码的执行。

so, if you include a false?所以,如果你包括一个假的? Then the page re-direct occurs, but not until that sub routine in that code stub is done running.然后页面重定向发生,但直到该代码存根中的子例程完成运行。

Edit:编辑:

There seems to be some confusing over the Response.Redirect("WebForm2"); Response.Redirect("WebForm2"); 似乎有些混乱。

in c#, if we go to definition, we get this: Lets look at the SECOND over-load:在 c# 中,如果我们对 go 进行定义,我们得到: 让我们看一下 SECOND 过载:

在此处输入图像描述

NO PLACE DO we see a suggested default exists for the 2nd value.没有地方我们看到第二个值存在建议的默认值。 (there is NOT a default) (没有默认值)

And going to the def we see this:转到 def 我们看到:

Now how there are TWO methods (2nd is a overload)现在有两种方法(第二个是重载)

在此处输入图像描述

And we can expand the 2nd one (without 2nd parameter).我们可以扩展第二个(没有第二个参数)。 We see this:我们看到这个:

在此处输入图像描述

and with the overload (with 2nd value), we see this:并且随着重载(具有第二个值),我们看到:

在此处输入图像描述

Once again, that 2nd parameter does NOT have a specified default.再一次,第二个参数没有指定的默认值。

And a simple quick test will confirm that:一个简单的快速测试将确认:

response.Redirect("some url")

Or或者

response.Redirect("some url",True)

BOTH produce the same effect - the code stub is terminated and exits.两者都产生相同的效果 - 代码存根被终止并退出。

If one wants the code stub to run out and continue to run, then using如果希望代码存根用完并继续运行,则使用

Response.Redirect("some url", false)

is required.是必须的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM