javascript 如何在javascript中编码url并在C#中解码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4613430/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 14:17:44  来源:igfitidea点击:

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

c#javascripturldecode

提问by Poornima

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

我有一个带有查询字符串的 url,通过它传递一些数据。我想在服务器端检索数据。这个问题的解决方案是什么

回答by eHussain

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

侯赛因

回答by ndtreviv

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.

您可以使用转义 ( 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)

回答by Raj kumar

  • Javascript unescape(_stringEncoded)same as HttpUtility.UrlDecode(_string)in C#
  • Javascript escape(_setringDecoded)same as HttpUtility.UrlEncode(_string)in C#
  • Javascript unescape(_stringEncoded)与C# 中的HttpUtility.UrlDecode (_string)相同
  • 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