简体   繁体   English

如何在javascript中对url进行编码并在C#中对其进行解码

[英]How to encode url in javascript and decode it in C#

I have a url with querystrings through which some data are passed. 我有一个带有查询字符串的URL,通过它可以传递一些数据。 I want to retrieve the data in the server side. 我想检索服务器端的数据。 What is the solution for this problem 这个问题的解决方案是什么?

You can use javascript's escape function to encode the URL. 您可以使用javascript的转义函数对URL进行编码。

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

URL Decoding in C# using Uri.UnescapeDataString() function. 使用Uri.UnescapeDataString()函数在C#中进行URL解码。

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 在C#中解析查询参数

NameValueCollection qscoll = HttpUtility.ParseQueryString(querystring);

Hope this will help. 希望这会有所帮助。

Thanks! 谢谢!

Hussain 侯赛因

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. 您可以使用escape( http://www.w3schools.com/jsref/jsref_escape.asp )或encodeURI( http://www.w3schools.com/jsref/jsref_encodeuri.asp )在Javascript端进行编码。

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 ) 在服务器端:对于C# - 使用System.Web.HttpUtility.UrlDecode进行解码( http://msdn.microsoft.com/en-us/library/adwtk1fy.aspx )对于Java - 使用URLDecoder进行解码( http:// download.oracle.com/javase/1.5.0/docs/api/java/net/URLDecoder.html )对于PHP - 使用urldecode( http://php.net/manual/en/function.urldecode.php

  • Javascript unescape(_stringEncoded) same as HttpUtility.UrlDecode(_string) in C# Javascript unescape(_stringEncoded)与C#中的HttpUtility.UrlDecode (_string)相同
  • Javascript escape(_setringDecoded) same as HttpUtility.UrlEncode(_string) in C# Javascript 转义(_setringDecoded)与C#中的HttpUtility.UrlEncode (_string)相同

Encode/Decode both 对两者进行编码/解码

javascript Encode javascript编码

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

C# Decode C#解码

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM