如何编码包含Unicode的URL?我想将其传递给一个命令行实用程序,并且需要先对其进行编码。
例如:http://zh.wikipedia.org/wiki/白雜訊
变成为http://zh.wikipedia.org/wiki/%E7%99%BD%E9%9B%9C%E8%A8%8A
。
如何编码包含Unicode的URL?我想将其传递给一个命令行实用程序,并且需要先对其进行编码。
例如:http://zh.wikipedia.org/wiki/白雜訊
变成为http://zh.wikipedia.org/wiki/%E7%99%BD%E9%9B%9C%E8%A8%8A
。
System.Web
程序集中的HttpUtility.UrlPathEncode
方法(需要完整的.NET Framework 4配置文件):var encoded = HttpUtility.UrlPathEncode("http://zh.wikipedia.org/wiki/白雜訊");
var urlString = Uri.EscapeUriString("http://zh.wikipedia.org/wiki/白雜訊");
Server.UrlEncode(s);
.NET 字符串本质上是 Unicode 字符串(具体来说是 UTF-8 编码),因此您只需要调用 HttpServerUtility.UrlEncode 即可(尽管在大多数 ASP.NET 上下文中,所谓的“内置” Server 属性都可用于此操作)。
<a href="/@Html.Raw(string)"
解决了这个问题。