Reputation: 48
Trying to join a list of strings together using string.join. When I use the Separator string " OR " the white spaces are being replaced with "+" which is breaking my targetUri string. Below is the code used to 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;
I have done many searches on Google but haven't been able to find anything that talks about string.join replacing characters. And during my debugging I was able to narrow it down to on the termsString line as where the problem occurs.
Here is an actual example of the string I get out: terms=value1+OR+value2+OR+value3
How would I stop the white spaces from being replaced with + characters?
Cheers,
James
Upvotes: 0
Views: 388
Reputation: 32561
In order to get the URL decoded value on the server side, you should use:
var encoded = "terms=value1+OR+value2+OR+value3";
var decoded = System.Web.HttpUtility.UrlDecode(encoded);
@PanagiotisKanavos, regarding my previous suggestion of using %20
instead of space, take a look at this JS:
var uri1="terms=value1%20OR%20value2%20OR%20value3";
var uri2="terms=value1+OR+value+OR+value3";
document.write(decodeURIComponent(uri1));
document.write("<br/>");
document.write(decodeURIComponent(uri2));
If you run it, you'll see that encoding could be sensitive in some contexts.
Upvotes: -1