Get values of a string in c #

1

I'm trying to get the values of a string that contains a url with a QueryString .

This is an example of the URL:

string url = "http://example.com?id=gdjh48vnnnvwsid1dkif84ndn?id2=cjodfnuvbvmf47747";

How do you see it is a string and not a QueryString since it is not the address of the page where I am but a url that I get with HTTPRequest .

I would like to know if there is any way to parse and get the values of the url (in this case id1 and id2 ) without using Split , Replace , etc ... since this is not guarantee that it works 100% of the time (in the value of id1 , for example, id1 appears).

    
asked by Miquel Coll 20.07.2016 в 09:41
source

1 answer

2

You can use the ParseQueryString method of the HttpUtility class.

Remember that you must add a reference to the System.Web assembly if you do not already have it in your project.

        string url = @"http://example.com?id=gdjh48vnnnvwsid1dkif84ndn&id2=cjodfnuvbvmf47747";
        Uri uri = new Uri(url);

        var queryString = HttpUtility.ParseQueryString(uri.Query);

        foreach (var key in queryString.Keys)
        {
            Console.WriteLine($"{key}: {queryString[(string)key]}");
        }
    
answered by 20.07.2016 / 10:19
source