我最近创建了一个关于如何在 URL 中使用像/和这样的符号的+问题,但这让我想到了另一个问题,我如何替换 URL 中的空格,为什么?
如果我的网址是something.com/Find/this is my search,那为什么是错误的?为什么我们需要将其更改为something.com/Find/this+is+my+search
我已经搜索和尝试解决方案超过 5 个小时了。我看的每个地方的答案都是一样的,使用httputility.urlencodeor Uri.escapeDataString。但我试过这样做:
string encode = Uri.EscapeDataString(TextBoxSearch.Text);
Response.Redirect("/Find/" + encode );
string encode = HttpUtility.UrlEncode(TextBoxSearch.Text);
Response.Redirect("/Find/" + encode );
string encode = encode.replace(" ", "+")
Response.Redirect("/Find/" + encode);
Run Code Online (Sandbox Code Playgroud)
这些都不起作用,它们不会用任何东西替换空格(string.replace 会这样做,但这也会导致字符串更改,这意味着它无法在下一页的数据库中找到值)。
如果我对整个 URL 进行编码,那么我/将全部转为 %,这显然不是我想要的。
我需要的
If I redirect like this Response.Redirect("/Find/" + search);.
And I make a search like this "Social media".
I then get the queryString on …Run Code Online (Sandbox Code Playgroud)