【问题标题】:Convert string with special characters to URL in class file, in .net4.0 [duplicate]在.net4.0中将带有特殊字符的字符串转换为类文件中的URL [重复]
【发布时间】:2014-05-25 03:24:31
【问题描述】:

我下面有一个字符串,需要在class文件中将其转换为URl,它包含特殊字符。

"http://www.GenerateURL.com/try?origins=**Rue 66 & Rue Oued Draa / Rue 77, Tangier, Morocco**&destinations=**Boulevard Lalla Asmaa, Casablanca, Morocco**&language=en-EN&sensor=false"

非常感谢

【问题讨论】:

  • 你的意思是替换特殊字符。
  • 我不明白“类文件中的 URl”是什么意思。你想用值填充 C# 类,还是编码为 URI?
  • 是替换特殊字符,在c#类文件中
  • 您显示的字符没有什么特别之处...使用正确的方法从重复的错误中构造查询字符串并避免任何字符串格式/连接。

标签: c# asp.net .net string url


【解决方案1】:

您可以使用HttpUtility.UrlEncode()。这是文档: http://msdn.microsoft.com/en-us/library/system.web.httputility.urlencode.aspx

【讨论】:

【解决方案2】:

您需要为此使用 System.Web.HttpUtility.UrlEncode:

string encodedString ="http://www.GenerateURL.com/try?origins=**Rue 66 & Rue Oued Draa / Rue 77, Tangier, Morocco**&destinations=**Boulevard Lalla Asmaa, Casablanca, Morocco**&language=en-EN&sensor=false";

 string real = System.Web.HttpUtility.UrlEncode(encodedString);

使用 Server.UrlEncode

您需要一个 HttpServerUtility 类的实例,因为 UrlEncode 方法不是静态的。

http://msdn.microsoft.com/en-us/library/system.web.httpserverutility(v=VS.90).aspx

【讨论】:

    【解决方案3】:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-10
      • 1970-01-01
      • 2016-01-30
      • 1970-01-01
      • 2013-03-08
      相关资源
      最近更新 更多