How should i write Response.Redirect() in asp.net
In asp.net two overload method of Response.Redirect() exist.
开发者_运维技巧
Public Sub Redirect ( _ url As String )
Public Sub Redirect ( _ url As String, _ endResponse As Boolean _ )
I would like to know the diffrence between these two? and which one should i use?
The first overload redirects to another URL, the second allows you to say whether the current code should continue to execute e.g. if Response.Redirect("http://philippursglove.com", True)
occurs in the middle of a block of code, the rest of the block of code will keep executing and run database updates or whatever.
As to which one you should be using, we can't tell you without seeing it in the context of a bit more of your code.
Also have a look at Server.Transfer, which achieves much the same thing as Response.Redirect but without sending anything to the browser, which can take a bit of pressure off your Web server. See Server.Transfer vs Response.Redirect.
They both send your browser a 302 response telling it to request the specified page. You usually don't want the response to continue if you are redirecting someone to a new page so by default that is what Response.Redirect("/") does.
If you do want to continue processing a response though you will need to set the second parameter to false.
So in this example a
will be 1:
var a = 1;
Response.Redirect("/aboutus.aspx");
a = 2;
In this example a
will be 2 because the thread keeps running after the redirect.
var a = 1;
Response.Redirect("/aboutus.aspx", false);
a = 2;
Careful though if using this in a try catch. A slight oddity means that in the next example a
will be 2!
var a = 1;
try
{
Response.Redirect("/aboutus.aspx");
}
finally
{
a = 2;
}
精彩评论