英文:
Response.Redirect plus Response.End
问题
这是我不完全理解的代码,所以我得出的结论是我漏掉了某些东西,或者代码只是一个错误,我想知道两者之间的区别。
这是我不理解的代码:
this.Response.Redirect('url', false);
this.Response.End();
它不是等同于:
this.Response.Redirect('url');
如果是这样,为什么要写两个语句而不是一个呢?我无法理解 Response.End()
的目的,感觉我对这些命令的含义有所遗漏。
this.Response.Redirect('url', false);
--> 应该重定向而不终止当前线程
this.Response.Redirect('url', true);
--> 应该重定向,并使用 ThreadAbortException
终止当前线程
this.Response.Redirect('url');
--> 应该重定向,并使用 ThreadAbortException
终止当前线程
this.Response.End();
--> 应该使用 ThreadAbortException
终止当前线程,如果此操作失败,则将当前响应作为其原样刷新到客户端。
--编辑:更多背景信息
正如评论中所指出的,如果我添加一些背景信息会更好。我发现这段代码所在的项目是一个ASP.NET Web Forms项目。该代码出现在按钮点击事件的服务器端处理程序中(在页面的代码后端,即aspx.cs文件中)。
英文:
I am reading code i don't fully understand, so i came to the conclusion that there is something i am missing or the code is just a mistake, and i would understand which of the two.
This is the code i don't understand :
this.Response.Redirect('url', false);
this.Response.End();
Isn't it equivalent to :
this.Response.Redirect('url');
if so why write two statement instead of one? I can't understand the pourpose of Response.End() and have the feeling that there is something i miss about the meaning of those commands.
this.Response.Redirect('url', false);
--> Should redirect without terminating current thread
this.Response.Redirect('url', true);
--> Should redirect, terminating current thread using a ThreadAbortException
this.Response.Redirect('url');
--> Should redirect, terminating current thread using a ThreadAbortException
this.Response.End();
--> Should terminate current thread using a ThreadAbortException, if this operation fail, then current response is flushed as is to client.
--EDIT : more context
As comment have pointed out is better if i add a bit of a context.
The project in which i find the code is an ASP.NET Web Form project. The code appear in the server side handler of a button click event (in the code bheind of the page, so in a aspx.cs).
答案1
得分: 1
简单来说,是的,这几乎是相同的:
this.Response.Redirect('url', false);
this.Response.End();
与以下代码段相同:
this.Response.Redirect('url');
或者
this.Response.Redirect('url', true);
那么,为什么会写两者,而它们做的事情却相同呢?
问题不在于它们何时做相同的事情,而在于当你想要做一些不同的事情时!!!
我的意思是拿这个例子来说:
void funtest()
{
// 这里有一些代码
return;
}
或者
void funtest()
{
// 这里有一些代码
}
那么,为什么还要写“return”,当它们两者都做相同的事情呢?
答案:你可能不想做相同的事情!!
因此,你可能会这样做:
void funtest()
{
if (某些代码条件)
return; // 放弃,不执行后面的代码
// 这里有一些代码
}
所以,第一个例子会显得有些愚蠢,因为我们既不关心也不需要返回语句,但在第二种情况下,哇,是时候给妈妈打电话了,这会产生巨大的差异。
因此,就像“return”语句“通常”(大多数情况下)不是必需的一样,在你的代码中有一些非常好的用例,你确实想要返回,因此作为开发人员,你可以、也应该使用“return”语句。使用“return”语句可以在何时退出该例程时给你更多的控制。
Response.End()也完全相同。
在大多数情况下,就像你不关心也不需要返回语句来结束代码例程一样,有时你确实想要控制代码,因此使用“return”语句。
因此,Response.End()也是如此。它为你作为开发人员提供了更多的控制权。
因此,可以这样说:
void funtest()
{
Response.Write("你好,你好吗");
}
或者
void funtest()
{
Response.Write("你好,你好吗");
Response.End();
}
现在,如果你只调用上面的一个例程,并且你很高兴网页现在要发送到客户端(Response.End()将把网页发送到客户端,并终止任何进一步的运行代码)。
所以,在许多情况下,Response.End()就像return语句一样。在大多数情况下,不关心,不担心,不需要。
然而,有时候你想要控制“返回”,所以你使用return语句并强制执行。它也使你作为开发人员的意图非常清晰。
同样适用于使用Response.End(),实际上它是你的“请发送浏览器副本回客户端”的方式 - 在服务器上我们已经完成了,我不希望在服务器端发生任何其他事情。
关键概念不在于这两个代码示例何时做相同的事情,而在于当你想在这里做一些不同的事情时!!!
我的意思是,我们可以使用
i = i + 1;
或者
i++;
那么,为什么有两种做相同事情的方式呢?
嗯,当你想要做一些不同的事情时。
所以
i = i + 100;
嗯,用i++,现在只会加1,这不是我想做的事情!!
因此,比较两个做相同事情的代码部分,无论是添加+1,还是上述方式?比较两种情况何时做相同的事情毫无意义。
但是,当我们想要不同的行为时?现在我们正在谈论!!!
换句话说,不要发布两个做相同事情的代码示例(例如将+1添加到一个值),然后问为什么选择其中一个?
答案:这可能并不重要!!
然而,当你想要不同的行为时,我们才有了使用两种不同方法的理由和故事。
你可以在你的代码中有“许多”Response.Write("一些文本"),但只有在所有代码完成后,页面才会传回用户桌面并进行显示+呈现。
当你构建并使用Response.Write("一些文本")时,最终用户将不会看到这些Response.Write的任何结果,直到页面的所有代码100%完成运行,并且IIS服务器释放了该网页并发送回客户端。
因此,Response.End()强制并允许你控制页面“完成”并且将要“发送”的时间。
同样适用于这样:
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("~/Test/StepProcess.aspx", false);
System.Threading.Thread.Sleep(5000);
}
所以,服务器将重定向到另一个页面,但仅在接下来的代码完成后才会发生!!!
所以,在99%的情况下,当你执行Response.Redirect("某个页面")时,你可能在99%的情况下希望当前代码停止运行!
但是,如上所示?只有在当前所有代码100%完成并且已“退出”或实际上“返回”时,响应重定向才会发生。(另一页的页面加载事件将等到当前页面的所有代码完全完成并且
英文:
the simple matter is YES this is quite much the same thing:
this.Response.Redirect('url', false);
this.Response.End();
as this:
this.Response.Redirect('url');
or
this.Response.Redirect('url',true);
So, why write both when they do the same?
Well, the issue is NOT WHEN they do the same, but WHEN YOU want to do something different!!!!
I mean take this example:
void funtest()
{
// some code here
return;
}
or
void funtest()
{
// some code here
}
So, why bother with "return" WHEN they both do the same thing?
Answer: You might NOT want to do the same thing!!!!
So, you might do this:
void funtest()
{
if (some code condition)
return; // bail out, don't run the code that follows
// some code here
}
So, while the first example would be silly, since we don't care nor need the return statement, but in the 2nd case, WOW, time to call momma, it makes a HUGE difference.
So, just like the return statement "often" (most of the time) is NOT required, there are some VERY good use cases in which in your code YOU DO want to return, and thus as a developer, you can and would and should use the return statement. Use of the "return" statement gives you MUCH MORE control when that routine will exit.
100% same goes for the Response.End().
In most cases, just like you don't care nor need a return statement to end the code routine, sometimes YOU DO want that control of the code, and thus use the "return" statement.
And thus the same goes for Response.End(). It gives you the developer more control.
So, say this:
void funtest()
{
Response.Write("Hello how are you");
}
or
void funtest()
{
Response.Write("Hello how are you");
Response.End();
}
Now, if you calling JUST the one above routine and you happy the web page is NOW to be sent to the client side (Response.End() will SEND the web page to the client, and terminal any more running code).
So, in many ways, the Response.End() is MUCH like the return statement. Most of the time, don't care, don't worry, don't need.
However, there ARE times when you want to control the "return", so, you use the return statement and force the issue. It also makes your intention as a developer VERY clear.
Same goes for using Response.End(), it in effect is your "please send the browser copy back to the client" - we are done on the server, and I don't want anything more to occur on the server side.
The key concept is NOT WHEN the 2 code examples do the same thing, but WHEN YOU want to do something different here!!!
I mean, we can use
i = i + 1;
or
i++;
So, why have 2 ways of doing the same thing?
Well, WHEN YOU want to do something different.
So
i = i + 100;
Well, with i++, NOW that only adds 1, and that's not what I want to do!!!
So, comparing 2 parts of code that does the same thing, be it adding + 1, or above? Makes ZERO sense to compare 2 cases WHEN they do the same thing.
But, WHEN we want different behavior's ? Now we are talking!!!!
In other words, don't post 2 code examples that do the same thing (like adding + 1 to a value), and then ask why choose one or the other?
Answer: it probably don't matter much!!
however, WHEN you want different behavior's, THEN we have a reason and story for the 2 different approaches.
You can have "many" Response.Write("some text") in your code, but only AFTER ALL CODE is done, does the page travel back to the user desktop and display + render.
As you build up and use Response.Write("some txt"), the end user will not see ANY of those response.Writes UNTILL ALL of the page code is 100% done running,, and the IIS server releases that web page and sends back to the client.
So, Response.End() forces and allows you control when the page is "done" and to be "sent".
Same goes for this:
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("~/Test/StepProcess.aspx",false);
System.Threading.Thread.Sleep(5000);
}
So, the server will redirect to the other page, but ONLY AFTER your code that follows is complete!!!
So, in 99% of cases, when you do a Response.Redirect("some page"), you probably in 99% of cases want the current code to stop running!
but, as above shows? The response.redirect DOES NOT occur until all of the current code is done! (the page load event of the other page will not start until such time all code in the current page is 100% complete and has "exit" or in fact "returned". in above, that means the code will wait 5 seconds, AND THEN do the response.redirect.
So, just keep in mind that "Response.Write", Response.Redirect() etc is ONLY really going to update the current copy of the web page on the server that is being processed. You ONLY see the results of "writing" to the web page WHEN the page travels back to the client side (and that includes Response.Write().
Remember, Response.Write() INJECTS code in the browser to jump to the other page!!!
Ok, can we jump to another page and NOT use the Response.Write approach?
yes, you can do a server side re-direct to another page.
eg:
Server.TransferRequest("~/Test/StepProcess.aspx");
In above, the browser page is NOT sent to the client side, but in fact the server now jumps to the other web page. (but, it still going to run that 2nd line of code, and still wait until the code in current page is done running).
In above, this also means that no say JavaScript or any code in the current page will ever get a chance to run, since the current page WILL NOT be sent back to the client side.
You often in many cases need to control WHEN that page is 100% done, and will THEN be sent to the client side.
thus, in most cases, you don't care or worry about a Response.End(), but in some cases, you most certainly do want to control WHEN all the code stops, and the page is to be sent to the client side.
So, the issue is not WHEN the code does the same thing, it when YOU want something different to occur!!! Be it adding +1 with "++" or using Response.Redirect(), the issue not when they do the same thing, but when you want to control and have DIFFERENT behavior's is the REASON to use one or the other.
答案2
得分: 0
Response.Redirect('url', false)
是一种方法,基本上是说:重定向到一个新页面,但不终止连接。 Redirect
会调用End
,如果第二个参数为空或设置为true,它会在完成时抛出ThreadAbortException
异常。这个异常对Web应用程序的性能有害。
Response.End()
是一种更"受控"的方法来终止连接,更详细的解释可以在这里看到。这仅用于与ASP的兼容性,当它尝试通过引发ThreadAbortException
异常来模仿ASP中End
方法的行为时。如果End
方法无法引发此异常,它会将响应字节同步刷新到客户端。这也可能对性能产生不利影响。
因此,这并不是一种常见的做法,如果目标不是与ASP兼容,我会将其视为错误。
英文:
Ok, so basically you have two concepts there:
Response.Redirect('url', false)
is a method that basically says: Redirect to a new page and do not end the connection. Redirect
calls End
which throws a ThreadAbortException
exception upon completion if the second parameter is null or set to true. This exception has a detrimental effect on Web application performance.
Response.End()
is a more "controlled" way to end the connection, a more in-depth explanation can be seen here. This can be used exclusively for compatibility with ASP, when it tries to mimic the behavior of the End
method in ASP by raising a ThreadAbortException
exception. If the End
method is not able to raise this exception, it instead flushes the response bytes to the client. It does this synchronously, which can also be detrimental to your performance also.
So, it's not a common practice at all and I would classify that as a error if the objective was not to do compatibility with ASP.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论