如何在C#中将所有空格替换为%20?

问题描述 投票:0回答:11

我想使用 C# 将字符串制作成 URL。 .NET 框架中一定有一些东西可以提供帮助,对吗?

c# url urlencode
11个回答
130
投票

另一种方法是使用

Uri.EscapeDataString(stringToEscape)


58
投票

我相信您正在寻找 HttpServerUtility.UrlEncode

System.Web.HttpUtility.UrlEncode(string url)

51
投票

我发现很有用

System.Web.HttpUtility.UrlPathEncode(string str);

它将空格替换为 %20 而不是 +。


27
投票

要正确转义空格以及其余特殊字符,请使用

System.Uri.EscapeDataString(string stringToEscape)


19
投票

正如对已批准故事的评论,HttpServerUtility.UrlEncode 方法用 + 代替 %20 替换空格。 请改用以下两种方法之一: Uri.EscapeUriString()Uri.EscapeDataString()

示例代码:

HttpUtility.UrlEncode("https://mywebsite.com/api/get me this file.jpg")
//Output: "https%3a%2f%2fmywebsite.com%2fapi%2fget+me+this+file.jpg"

Uri.EscapeUriString("https://mywebsite.com/api/get me this file.jpg");
//Output: "https://mywebsite.com/api/get%20me%20this%20file.jpg"
Uri.EscapeDataString("https://mywebsite.com/api/get me this file.jpg");
//Output: "https%3A%2F%2Fmywebsite.com%2Fapi%2Fget%20me%20this%20file.jpg"

//When your url has a query string:
Uri.EscapeUriString("https://mywebsite.com/api/get?id=123&name=get me this file.jpg");
//Output: "https://mywebsite.com/api/get?id=123&name=get%20me%20this%20file.jpg"
Uri.EscapeDataString("https://mywebsite.com/api/get?id=123&name=get me this file.jpg");    
//Output: "https%3A%2F%2Fmywebsite.com%2Fapi%2Fget%3Fid%3D123%26name%3Dget%20me%20this%20file.jpg"

3
投票

我也需要这样做,几年前就发现了这个问题,但问题标题和文本不太匹配,并且使用

Uri.EscapeDataString
UrlEncode
(请不要使用那个!)通常不会使除非我们正在讨论将 URL 作为参数传递给其他 URL,否则这是有意义的。

(例如进行开放ID认证、Azure AD等时传递回调URL)

希望这是对这个问题更务实的答案:我想使用 C# 将字符串转换为 URL,.NET 框架中一定有一些东西可以提供帮助,对吧?

- 有两个函数有助于在 C# 中制作 URL 字符串

  • String.Format
    用于格式化 URL
  • Uri.EscapeDataString
    用于转义 URL 中的任何参数

这段代码

String.Format("https://site/app/?q={0}&redirectUrl={1}", 
  Uri.EscapeDataString("search for cats"), 
  Uri.EscapeDataString("https://mysite/myapp/?state=from idp"))

产生这个结果

https://site/app/?q=search%20for%20cats&redirectUrl=https%3A%2F%2Fmysite%2Fmyapp

可以安全地复制并粘贴到浏览器的地址栏,或 HTML

src
标签的
A
属性,或与
curl
一起使用,或编码成 QR 码等。



-1
投票

HttpUtility.UrlDecode 对我有用:

var str = "name=John%20Doe";
var str2 = HttpUtility.UrlDecode(str);

str2 =“姓名=约翰·多伊”



-2
投票

下面的代码将用单个 %20 字符替换重复空格。

示例:

输入为:

Code by Hitesh             Jain

输出:

Code%20by%20Hitesh%20Jain

代码

static void Main(string[] args)
{
    Console.WriteLine("Enter a string");
    string str = Console.ReadLine();
    string replacedStr = null;

    // This loop will repalce all repeat black space in single space
    for (int i = 0; i < str.Length - 1; i++)
    {
        if (!(Convert.ToString(str[i]) == " " &&
            Convert.ToString(str[i + 1]) == " "))
        {
            replacedStr = replacedStr + str[i];
        }
    }
    replacedStr = replacedStr + str[str.Length-1]; // Append last character
    replacedStr = replacedStr.Replace(" ", "%20");
    Console.WriteLine(replacedStr);
    Console.ReadLine();
}

-4
投票

HttpServerUtility.HtmlEncode

来自文档:

String TestString = "This is a <Test String>.";
String EncodedString = Server.HtmlEncode(TestString);

但这实际上编码的是 HTML,而不是 URL。而是使用 UrlEncode(TestString)

© www.soinside.com 2019 - 2024. All rights reserved.