Maha Khairy
Maha Khairy

Reputation: 352

send a url with multiple querystring as a querystring parameter in asp.net

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

Upvotes: 2

Views: 4628

Answers (3)

phil soady
phil soady

Reputation: 11348

Encode the URL

System.Web.HttpUtility.UrlEncode(string url)

Upvotes: 0

sreejithsdev
sreejithsdev

Reputation: 1210

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'");

Upvotes: 0

Anujith
Anujith

Reputation: 9370

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;

    }

Upvotes: 2

Related Questions