Ahmad
Ahmad

Reputation: 335

Query string changes when try to use it.

I have a query string that looks like this. Page.aspx?s=C94CA8CCAFB12E2B669735186D327D1B3E505538139A66C8455X%2353411389BBB9577E1FD Then I have a Button that has a post back that looks like this:

protected void btn_Click(object sender, EventArgs e)
{
    string URL = Request.QueryString["s"];
    Response.Redirect("Page2.aspx?s="+URL);
}

But when I get to the page 2 the query string that I put in URL changes the % sign into # and the decryption fails because it looks for that % in the string to separate all my pages works with that decryption so I cant change the way that works I need to know why does it change that % into # This is how it looks like when it goes to the Page2.aspx

Page2.aspx?s=C94CA8CCAFB12E2B669735186D327D1B3E505538139A66C8455X#53411389BBB9577E1FD

Upvotes: 3

Views: 263

Answers (4)

Simple Code
Simple Code

Reputation: 568

string URL = Request.QueryString["s"];
        URL = Server.UrlEncode(URL); // This will allow it to keep the %23 
        Response.Redirect("Page2.aspx?s="+URL);

As all the answers you got yes thats the reason Encoded Just make sure you tell it its a UrlEncode so it dont change that.

Upvotes: 3

squillman
squillman

Reputation: 13641

%23 is the url-encoded code for the # character. If you look, it's actually replacing the %23 with # and not just the %. You will need to check for that when passing query string information between pages.

protected void btn_Click(object sender, EventArgs e)
{
    string URL = Request.QueryString["s"];
    Response.Redirect("Page2.aspx?s="+UrlEncode(URL));
}

Upvotes: 4

Sefe
Sefe

Reputation: 14007

If you check your query string carefully, you will realize that not % is replaced by #, but %23. That is because the % is an escape character and %23 stands for #. You have to escape % itself with %25:

Page.aspx?s=C94CA8CCAFB12E2B669735186D327D1B3E505538139A66C8455X%252353411389BBB9577E1FD

Upvotes: 3

Herohtar
Herohtar

Reputation: 5613

It's being translated as a URL Encoded character. Note that it's not just the percent sign that is being replaced, but also the 23 that is following it. If you look at the table listed on the Wikipedia page you'll see that %23 is the # character.

Upvotes: 1

Related Questions