Encoding and decoding an asp.net query string

I type the following URL into my web browser and press Enter.

http://localhost/website.aspx?paymentID=6++7d6CZRKY%3D&language=English 

Now in my code, when I do HttpContext.Current.Request.QueryString ["paymentID"],

I get 6 7d6CZRKY =

but when I do HttpContext.Current.Request.QueryString.ToString (), I see the following:

PaymentID = 6 ++ 7d6CZRKY% 3D & language = English

I want to extract the actual payment identifier that the user entered in the URL of the web browser. I am not worried about whether the URL is encoded or not. Because I know that there is a strange thing happening here% 3D and + sign at the same time! But I need a + sign. Somehow it becomes decoded in space when I do HttpContext.Current.Request.QueryString ["paymentID"].

I just want to retrieve the actual payment id that the user dialed. What is the best way to do this?

Thanks.

+4
source share
3 answers

First you need to encode the URL first using URLEncode (). + in the url is equal to a space, so it must be encoded to% 2b.

 string paymentId = Server.UrlEncode("6++7d6CZRKY="); // paymentId = 6%2b%2b7d6CZRKY%3d 

And now

 string result = Request.QueryString["paymentId"].ToString(); //result = 6++7d6CZRKY= 

but

 string paymentId = Server.UrlEncode("6 7d6CZRKY="); //paymentId looks like you want it, but the + is a space -- 6++7d6CZRKY%3d string result = Request.QueryString["paymentId"].ToString(); //result = 6 7d6CZRKY= 
+5
source

There is information about this: Plus add a query string .

But I suppose you can also use regex to get your parameter from the query string. Something like that:

 string queryString = HttpContext.Current.Request.QueryString.ToString(); string paramPaymentID = Regex.Match(queryString, "paymentID=([^&]+)").Groups[1].Value; 
+1
source

I sent arabic text to the query string

arabic text in my query string

and when I received this string, it was encoded enter image description here

after Server.UrlDecode

  departmentName = Server.UrlDecode(departmentName); 

back to arabic enter image description here

I hope this helps you

+1
source

Source: https://habr.com/ru/post/1446826/


All Articles