HttpUtility.ParseQueryString Method

Definition

Parses a query string into a NameValueCollection.

Overloads

ParseQueryString(String)

Parses a query string into a NameValueCollection using UTF8 encoding.

ParseQueryString(String, Encoding)

Parses a query string into a NameValueCollection using the specified Encoding.

ParseQueryString(String)

Source:
HttpUtility.cs
Source:
HttpUtility.cs
Source:
HttpUtility.cs

Parses a query string into a NameValueCollection using UTF8 encoding.

C#
public static System.Collections.Specialized.NameValueCollection ParseQueryString(string query);

Parameters

query
String

The query string to parse.

Returns

A NameValueCollection of query parameters and values.

Exceptions

query is null.

Examples

The following code example demonstrates how to use the ParseQueryString method. Multiple occurrences of the same query string variable are consolidated in one entry of the returned NameValueCollection.

C#

using System;
using System.Web;

class Program
{
    static void Main()
    {
        // Parse the URL and get the query string
        var url = "https://www.microsoft.com?name=John&age=30&location=USA";
        var parsedUrl = url.Split('?')[1];

        // The ParseQueryString method will parse the query string and return a NameValueCollection
        var paramsCollection = HttpUtility.ParseQueryString(parsedUrl);

        // The foreach loop will iterate over the params collection and print the key and value for each param
        foreach (var key in paramsCollection.AllKeys)
        {
            Console.WriteLine($"Key: {key} => Value: {paramsCollection[key]}");
        }
    }
}

// The example displays the following output:
// Key: name => Value: John
// Key: age => Value: 30
// Key: location => Value: USA

Remarks

The ParseQueryString method uses UTF8 format to parse the query string In the returned NameValueCollection, URL encoded characters are decoded and multiple occurrences of the same query string parameter are listed as a single entry with a comma separating each value.

Important

The ParseQueryString method uses query strings that might contain user input, which is a potential security threat. By default, ASP.NET Web pages validate that user input does not include script or HTML elements. For more information, see Script Exploits Overview.

See also

Applies to

.NET 10 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

ParseQueryString(String, Encoding)

Source:
HttpUtility.cs
Source:
HttpUtility.cs
Source:
HttpUtility.cs

Parses a query string into a NameValueCollection using the specified Encoding.

C#
public static System.Collections.Specialized.NameValueCollection ParseQueryString(string query, System.Text.Encoding encoding);

Parameters

query
String

The query string to parse.

encoding
Encoding

The Encoding to use.

Returns

A NameValueCollection of query parameters and values.

Exceptions

query is null.

-or-

encoding is null.

Remarks

In the returned NameValueCollection, URL encoded characters are decoded and multiple occurrences of the same query string parameter are listed as a single entry with a comma separating each value.

Important

The ParseQueryString method uses query strings that might contain user input, which is a potential security threat. By default, ASP.NET Web pages validate that user input does not include script or HTML elements. For more information, see Script Exploits Overview.

See also

Applies to

.NET 10 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1