How to encode url in javascript and decode it in C#

匿名 (未验证) 提交于 2019-12-03 02:30:02

问题:

I have a url with querystrings through which some data are passed. I want to retrieve the data in the server side. What is the solution for this problem

回答1:

You can use javascript's escape function to encode the URL.

Example :  escape("It's me!") // result: It%27s%20me%21 

URL Decoding in C# using Uri.UnescapeDataString() function.

Example :  s = "%46%69%67%68%74%20%74%68%65%20%70%6F%77"; Uri.UnescapeDataString(s);  

EDIT -------------------------

To Parse Query parameters in C# use

NameValueCollection qscoll = HttpUtility.ParseQueryString(querystring); 

Hope this will help.

Thanks!

Hussain



回答2:

You can use escape ( http://www.w3schools.com/jsref/jsref_escape.asp ) or encodeURI ( http://www.w3schools.com/jsref/jsref_encodeuri.asp ) to encode on Javascript side.

On server side: For C# - Use System.Web.HttpUtility.UrlDecode to decode ( http://msdn.microsoft.com/en-us/library/adwtk1fy.aspx ) For Java - Use URLDecoder to decode ( http://download.oracle.com/javase/1.5.0/docs/api/java/net/URLDecoder.html ) For PHP - Use urldecode ( http://php.net/manual/en/function.urldecode.php )



回答3:

  • Javascript unescape(_stringEncoded) same as HttpUtility.UrlDecode(_string) in C#
  • Javascript escape(_setringDecoded) same as HttpUtility.UrlEncode(_string) in C#

Encode/Decode both

javascript Encode

escape('raj kumar') //raj%20kumar 

C# Decode

HttpUtility.UrlDecode("raj%20kumar") //raj kumar 


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!