0

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

1 Answers1

-1

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.

Alex Filipovici
  • 31,789
  • 6
  • 54
  • 78
  • String.Join does NOT encode anything. Replacing spaces with %20 will result in an additional encoding of the '%' when the result gets encoded. Besides, '+' is a valid url-encoding of spaces too – Panagiotis Kanavos Jul 03 '13 at 09:05
  • I know that and I never said that, but as the context about using the `termsString["terms"]` value is not given, I made a suggestion which might help for debugging. How do you know that there is no custom URL enconding involved? – Alex Filipovici Jul 03 '13 at 09:07
  • @PanagiotisKanavos, this is why I suggested the use of `%20`in the previous version of my answer: [http://stackoverflow.com/a/1211249/674700](http://stackoverflow.com/a/1211249/674700) (just to eliminate the possibility of such a _picky_ system). Here's a demo [http://jsfiddle.net/a4TnE/](http://jsfiddle.net/a4TnE/). – Alex Filipovici Jul 03 '13 at 09:28