2

I need to send a redirect url as a query string parameter, unfortunately this url contains more than one querystring parameter as follows

"http://host:port/page2.aspx?param1=value&param2=value&param3=value"

the problem is when I encode this url and send it via querystring , it looks as if it wasn't encoded , so my application consider the redirect url parameter value to be only

"http://host:port/page2.aspx?param1=value"

and consider

"&param2=value&param3=value"

as a part of the current url

I tried Server.UrlEncode and Server.HtmlEncode

3
  • Could you show your code? The one you have tried with Server.UrlEncode and which didn't work? Commented Dec 2, 2012 at 15:22
  • it did Encode the url, but once it is redirected the encoding just disappear and the query string looks as if it was never encoded with all the '&' chars visible in the url in browser if it might help you to know the url is constructed in code behind and added to a link button PostBackUrl property when the grid is bound, since the link button is in a grid view rows Commented Dec 2, 2012 at 15:25
  • string url = string.Format("~/MyFolder/Page.aspx?Id={0}&RedirectUrl={1}", Id, Server.UrlEncode(MyRedirectUrl)); Commented Dec 2, 2012 at 15:29

3 Answers 3

2
string myUrl = “http://host:port/page2.aspx?param1=value&param2=value&param3=value”;

string EncodedUrl = myUrl.EncodeTo64();

Pass this as querystring and retrieve using :

    EncodedUrl.DecodeFrom64();

Functions:

public static string EncodeTo64(this string target)
    {

        byte[] toEncodeAsBytes

              = System.Text.ASCIIEncoding.ASCII.GetBytes(target);

        string returnValue

              = System.Convert.ToBase64String(toEncodeAsBytes);

        return returnValue;

    }

public static string DecodeFrom64(this string target)
    {

        byte[] encodedDataAsBytes

            = System.Convert.FromBase64String(target);

        string returnValue =

           System.Text.ASCIIEncoding.ASCII.GetString(encodedDataAsBytes);

        return returnValue;

    }
Sign up to request clarification or add additional context in comments.

Comments

0

Replace '&' in your url with '%26' or use Server.UrlEncode

Responsse.redirect("redirectURL?a='http://url2?a=5%26b=7'&b=9'");
 or
Responsse.redirect("redirectURL?a="+Server.UrlEncode("http://url2?a=5&b=7")+"&b=9'");

Comments

0

Encode the URL

System.Web.HttpUtility.UrlEncode(string url)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.