Redirect from URL with QueryString to the same URL without QueryString

Posted by Slauma on Stack Overflow See other posts from Stack Overflow or by Slauma
Published on 2010-03-19T16:32:19Z Indexed on 2010/03/19 16:51 UTC
Read the original article Hit count: 656

Filed under:
|
|
|

I have a page request with a QueryString, say http://www.xyz.net/Orders.aspx?OrderID=1. The page is displayed in a browser. Now there is an asp:LinkButton on the page which should enable the user to open the page without the QueryString (as if he had entered http://www.xyz.net/Orders.aspx directly in the browser's address bar).

I had two ideas:

1) Use the PostBackUrl attribute of the LinkButton:

<asp:LinkButton ID="LinkButton1" runat="server" Text="Select"
    PostBackUrl="~/Orders.aspx" />

2) Use "RedirectUrl" in an event handler:

<asp:LinkButton ID="LinkButton1" runat="server" Text="Select"
    OnClick="LinkButton1_Click" />

...and...

protected void LinkButton1_Click(object sender, EventArgs e)
{
    Response.Redirect("~/Orders.aspx");
}

In both cases the browser's address bar shows http://www.xyz.net/Orders.aspx without the QueryString, as I like to have it. But in the first case the page does not change at all. But it should, because I'm evaluating the QueryString in code-behind and control the appearance of the page depending on whether a QueryString exists or not. The second option works as intended.

If I am not wrong the second option requires an additional roundtrip:

  • Browser sends request to server
  • Event handler on server side sends Redirect URL to browser
  • Browser sends again request to the server, but with the new URL
  • Server sends new requested page to browser

Is this correct at all?

Whereas the first option omits the first two steps in the list above, thus saving the additional roundtrip and resulting in:

  • Browser sends request to the server, but with the new URL (the PostbackURL specified in the LinkButton)
  • Server sends new requested page to browser

But, as said, the result isn't the same.

I'm sure my try to explain the differences between the two options is wrong somewhere. But I don't know where exactly.

Can someone explain what's really the difference? Do I really need this second roundtrip of option (2) to achieve what I want?

Thanks in advance!

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about redirect