嘗試使用string.join將字符串列表連接在一起。當我使用分隔符字符串「OR」時,空白符將被替換爲正在破壞我的targetUri字符串的「+」。以下是用於加入的代碼。使用string.join時字符串空白替換錯誤
if (DocumentSearchListViewModel.Filter == null)
{
return "http://000.000.00.00:8080/value/value/search/json?terms=value%20OR%20value&target=TEST2&maxResults=5";
}
var targetUri = "http://000.000.00.00:8080/value/value/search/json?";
NameValueCollection termsString = System.Web.HttpUtility.ParseQueryString(string.Empty);
if (!string.IsNullOrWhiteSpace(DocumentSearchListViewModel.Filter.Keywords))
{
if (!string.IsNullOrWhiteSpace(DocumentSearchListViewModel.Filter.Author))
{
DocumentSearchListViewModel.Filter.Keywords += (" " + DocumentSearchListViewModel.Filter.Author);
}
IList<string> keywords = DocumentSearchListViewModel.Filter.Keywords.Split();
termsString["terms"] = string.Join(" OR ", keywords);
}
targetUri += termsString.ToString();
targetUri += "&target=TEST2&maxResults=";
targetUri += DocumentSearchListViewModel.Filter.MaxNumberOfResults ?? "5";
return targetUri;
我在Google上做了很多搜索,但一直沒能找到任何有關string.join替換字符的內容。在我的調試過程中,我能夠將其縮小到條件字符串行,作爲發生問題的位置。
這裏是字符串我出去的實際例子:條件=值1 + OR +值2 + OR + VALUE3
如何我將停止從+字符被替換的空格?
乾杯,
詹姆斯
'+'被添加到URL中。您是否在代碼的其他部分對結果進行了URL編碼?如果你設置了一個斷點並檢查string.Join返回的值,我敢打賭它不包含任何'+'字符 –
你如何使用'termsString [「terms」]的值? –
問題不在'string.Join'中,你的關鍵字必須以某種方式包含'+',否則它會被替換。 –