Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an C# equivalent to the PHP function `parse_str`?

Tags:

c#

php

parsing

Is there an C# equivalent to the PHP function parse_str?

I couldn't find anything and wrote my own function but is there something in the C# framework?

public Dictionary<string, string> parse_str(string query) {

    Dictionary<string, string> data = new Dictionary<string, string>();

    foreach(string set in query.Trim('?').Split('&'))
        data.Add(set.Split('=')[0], set.Split('=').Length < 2 ? "" : set.Split('=')[1]);

    return data;
}​​​​​​
like image 716
iambriansreed Avatar asked May 16 '26 00:05

iambriansreed


2 Answers

I think you are looking for HttpUtility.ParseQueryString()

like image 69
DaveRandom Avatar answered May 17 '26 12:05

DaveRandom


If you're taking it from the browser's query string, you can use Request.QueryString

You can get a list of all the keys: Request.QueryString.Keys

Get a value of a key: Request.QueryString["KeyName"]

like image 38
Khan Avatar answered May 17 '26 14:05

Khan