I’m trying to generate an url like,
I have been using StringBuilder so far for the task but I’d like to think it’s not the appropriate way of doing it. As a result, I came to conclusion that I should be generating this link using Uri class and not StringBuilder or any string extensions. Soon after, I came across QueryHelpers.AddQueryString. The issue with that is, it’s using Dictionaries to add query parameters and adding an identical parameter (‘filter’ in my example) is just not possible.
Just wondering is there any other built in function or library that I can use to cleanly generate my urls with identical query parameters?
>Solution :
One of the overloads to QueryHelpers.AddQueryString takes an enumerable of key/value pairs. This means you can create your own ‘dictionary’ like this:
var filters = new List<KeyValuePair<string, string>>
{
new KeyValuePair("filter","testFilter1"),
new KeyValuePair("filter","testFilter2"),
new KeyValuePair("filter","testFilter3"),
}
Then you can call
var uri = QueryHelpers.AddQueryString("www.example.com", filters);