Search code examples
c#.netparsingurl-encodingstandard-library

How to extract specific-case fields from HttpUtility.ParseQueryString?


The .net standard library function System.Web.HttpUtility.ParseQueryString parses a query string into name/value pairs.

var a = HttpUtility.ParseQueryString("abc=123&def=456");
var x = a["abc"]; /* "123" */

When there are two fields with the same names except they differ by case, the returned object combines both values together with a comma.

var a = HttpUtility.ParseQueryString("abc=123&ABC=456");
var x = a["abc"]; /* "123,456" */

How can I pull out just the "abc" part on its own? Hopefully without rewriting the parser or pre-processing the string.


Solution

  • Having two keys with same names with different case is not possible with parseQuerystring as its return type is NameValueCollection whose default behavior is to provide a collection of unique keys and if you try to add same key in this collection it will by default add is at comma separated with existing key value .

    Let me know if this helps.