【发布时间】:2015-05-10 10:18:24
【问题描述】:
我最近创建了一个关于如何在 URL 中使用 / 和 + 等符号的问题,但这让我想到了另一个问题,如何替换 URL 中的空格,为什么?
如果我的网址是something.com/Find/this is my search,那为什么错了?为什么要改成something.com/Find/this+is+my+search
我已经搜索和尝试解决方案超过 5 个小时了。我在任何地方看到的答案都是一样的,使用httputility.urlencode 或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);
这些都不起作用,它们不会用任何东西替换空格(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 the next page and use it to load info from my database.
Now I want to display info about Social media from my database.
but at the same time I want the url to say Find/Social+media.
编辑:
我的尝试:
string encode = HttpUtility.UrlEncode(TextBoxSearch.Text);
Response.Redirect("/Find/" + encode);
这给了我一个“404.11 - 请求过滤模块被配置为拒绝包含双转义序列的请求。”在请求的 URL http://localhost:65273/Find/social+media
在我的 Find.aspx onLoad() 中:
IList<string> segments = Request.GetFriendlyUrlSegments();
string val = "";
for (int i = 0; i < segments.Count; i++)
{
val = segments[i];
}
search = val;
【问题讨论】:
-
这不是一个有效的网址,网址中的空格是
%20 -
当我在 localhost 上运行并使用空格进行搜索时,它工作正常。即使没有“+”替换。但是在我阅读之后我明白我不能在 url 中使用空格那么我该如何替换它们呢?
-
您的浏览器会将它们变成
%20,所以不用担心。 -
您的 URL 是如何被提取的?你使用 ASP.NET MVC 吗?
-
空格不比加号安全。