gpt4 book ai didi

c# - 从数据库编码 URL 查询字符串

转载 作者:太空宇宙 更新时间:2023-11-03 15:44:14 25 4
gpt4 key购买 nike

我正在尝试对作为查询字符串发送的搜索进行编码 (Response.Redirect("/Find/"+ TextBoxSearch.Text);)。数据库中有一行名称包括 /+,当它进入 URL 时,事情就会停止正常工作。我试过这样编码:

String encode = HttpUtility.UrlEncode(TextBoxSearch.Text);
Response.Redirect("/Find/" + encode);

但是不能让它工作,我错过了什么?假设搜索值为 ex/ex 18+。我怎样才能让它作为查询字符串工作?

不知道这是否重要,但这是我在查找页面中获取查询的方式:

IList<string> segments = Request.GetFriendlyUrlSegments();
string val = "";
for (int i = 0; i < segments.Count; i++)
{
val = segments[i];
}

search = val;

我什至无法正确编码空格。我尝试:

String encoded = Uri.EscapeDataString(TextBoxSearch.Text);
Response.Redirect("/Find/" + encoded);

但这不会将查询字符串中的空格转换为 %20。不过,它确实会转换“/”。

编辑:此时我很乐意将此 url localhost/Find/here are spaces 转换为 localhost/Find/here+are+spaces>/p>

编辑:我已经搜索并尝试了 5 个多小时的解决方案。谁能告诉我这个:

  • 如果我这样重定向 Response.Redirect("/Find/"+ search);
  • 然后我进行这样的搜索社交媒体
  • 然后我使用段获取 queryString 作为上面的代码。
  • 现在我想从我的数据库中显示有关社交媒体的信息
  • 但同时我希望 url 说 Find/Social+media

PS:我需要对每个 url 字符串进行编码吗?或者就在我使用符号和空格的地方。

最佳答案

而不是 HttpUtility.UrlEncode使用 HttpUtility.UrlPathEncode .来自 UrlEncode 的文档:

You can encode a URL using with the UrlEncode method or the UrlPathEncode method. However, the methods return different results. The UrlEncode method converts each space character to a plus character (+). The UrlPathEncode method converts each space character into the string "%20", which represents a space in hexadecimal notation. Use the UrlPathEncode method when you encode the path portion of a URL in order to guarantee a consistent decoded URL, regardless of which platform or browser performs the decoding.

+ 字符确实 表示查询字符串中的空格,但不表示地址部分中的空格。

关于c# - 从数据库编码 URL 查询字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28937572/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com