Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Using identical query params with QueryHelpers

I’m trying to generate an url like,

www.example.com/Example?someOption=true&anotherOption=false&filter=testFilter&filter=testFilter2&filter=testFilter3

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading