将包含特殊字符的字符串转换为URL,位于.net4.0的类文件中。

8 浏览
0 Comments

将包含特殊字符的字符串转换为URL,位于.net4.0的类文件中。

我有一个字符串,需要在类文件中将其转换为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"

非常感谢。

0
0 Comments

问题的出现原因是需要将包含特殊字符的字符串转换为URL格式,但是不知道如何实现。解决方法是使用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类的对象进行调用。

参考链接:[http://msdn.microsoft.com/en-us/library/system.web.httpserverutility(v=VS.90).aspx](http://msdn.microsoft.com/en-us/library/system.web.httpserverutility(v=VS.90).aspx)

0
0 Comments

在.NET 4.0中,如果需要将带有特殊字符的字符串转换为URL,可以使用HttpServerUtility.UrlEncode方法来实现。

HttpServerUtility.UrlEncode方法是一个.NET提供的用于URL编码的方法。它将特殊字符转换为URL编码表示,以便在URL中使用。

在使用HttpServerUtility.UrlEncode方法时,只需要将需要转换的字符串作为方法的参数传入即可。方法会返回一个URL编码后的字符串。

以下是使用HttpServerUtility.UrlEncode方法的示例代码:

string originalString = "hello world!";
string encodedString = HttpServerUtility.UrlEncode(originalString);
Console.WriteLine(encodedString);

上述代码中,我们将字符串"hello world!"传入HttpServerUtility.UrlEncode方法中进行URL编码。编码后的结果为"hello%20world%21"。

需要注意的是,HttpServerUtility.UrlEncode方法只会对特殊字符进行编码,不会对字母、数字和其他常见字符进行编码。这使得编码后的URL依然具有可读性。

使用HttpServerUtility.UrlEncode方法可以确保在URL中使用特殊字符时不会引发错误,同时也可以保证URL的正确性和可读性。

总结起来,问题的出现是因为需要将带有特殊字符的字符串转换为URL。解决方法是使用.NET 4.0中提供的HttpServerUtility.UrlEncode方法进行URL编码,以确保URL的正确性和可读性。通过将需要转换的字符串作为方法的参数传入,即可获得URL编码后的字符串。

0
0 Comments

问题:在.NET 4.0的类文件中,如何将带有特殊字符的字符串转换为URL?

在C#类文件中,我找不到HttpUtility类。在你的C#文件的顶部,添加using System.Web;。使用WebUtility类。

解决方法:可以使用HttpUtility.UrlEncode()方法将带有特殊字符的字符串转换为URL。在C#类文件中,如果找不到HttpUtility类,可以在文件的顶部添加using System.Web;语句。然后,可以使用WebUtility类来进行URL编码。

具体步骤如下:

1. 在C#类文件的顶部添加using System.Web;语句。

2. 使用HttpUtility.UrlEncode()方法将带有特殊字符的字符串进行URL编码。

参考链接:

- HttpUtility.UrlEncode()方法的文档:http://msdn.microsoft.com/en-us/library/system.web.httputility.urlencode.aspx

- WebUtility类的文档:http://msdn.microsoft.com/en-us/library/system.net.webutility.aspx

0