HttpUtility.ParseQueryString 方法

定义

将查询字符串解析为 NameValueCollection

重载

ParseQueryString(String)

使用 UTF8 编码将查询字符串分析成一个 NameValueCollection

ParseQueryString(String, Encoding)

使用指定的 Encoding 将查询字符串分析成一个 NameValueCollection

ParseQueryString(String)

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

使用 UTF8 编码将查询字符串分析成一个 NameValueCollection

public:
 static System::Collections::Specialized::NameValueCollection ^ ParseQueryString(System::String ^ query);
public static System.Collections.Specialized.NameValueCollection ParseQueryString (string query);
static member ParseQueryString : string -> System.Collections.Specialized.NameValueCollection
Public Shared Function ParseQueryString (query As String) As NameValueCollection

参数

query
String

要分析的查询字符串。

返回

查询参数和值的 NameValueCollection

例外

querynull

示例

下面的代码示例演示如何使用 ParseQueryString 方法。 同一查询字符串变量的多个匹配项合并在返回 NameValueCollection的一个条目中。


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

Imports System.Collections.Specialized
Imports System.Web

Public Class Sample
    Public Shared Sub Main()
        ' Parse the URL and get the query string
        Dim url As String = "https://www.microsoft.com?name=John&age=30&location=USA"
        Dim parsedUrl As String = url.Split("?")(1)

        ' The ParseQueryString method will parse the query string and return a NameValueCollection
        Dim paramsCollection As NameValueCollection = HttpUtility.ParseQueryString(parsedUrl)

        ' The For Each loop will iterate over the params collection and print the key and value for each param
        For Each key As String In paramsCollection.AllKeys
            Console.WriteLine($"Key: {key} => Value: {paramsCollection(key)}")
        Next
    End Sub
End Class

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

注解

方法 ParseQueryString 使用 UTF8 格式分析查询字符串 在返回的 NameValueCollection中,对 URL 编码的字符进行解码,并且同一查询字符串参数的多次匹配项以逗号分隔每个值作为单个条目列出。

重要

方法 ParseQueryString 使用可能包含用户输入的查询字符串,这是一种潜在的安全威胁。 默认情况下,ASP.NET 网页验证用户输入是否不包含脚本或 HTML 元素。 有关详细信息,请参阅脚本侵入概述

另请参阅

适用于

ParseQueryString(String, Encoding)

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

使用指定的 Encoding 将查询字符串分析成一个 NameValueCollection

public:
 static System::Collections::Specialized::NameValueCollection ^ ParseQueryString(System::String ^ query, System::Text::Encoding ^ encoding);
public static System.Collections.Specialized.NameValueCollection ParseQueryString (string query, System.Text.Encoding encoding);
static member ParseQueryString : string * System.Text.Encoding -> System.Collections.Specialized.NameValueCollection
Public Shared Function ParseQueryString (query As String, encoding As Encoding) As NameValueCollection

参数

query
String

要分析的查询字符串。

encoding
Encoding

要使用的 Encoding

返回

查询参数和值的 NameValueCollection

例外

querynull

encodingnull

注解

在返回的 NameValueCollection中,将解码 URL 编码字符,并且同一查询字符串参数的多次匹配项将作为单个条目列出,每个值都用逗号分隔。

重要

方法 ParseQueryString 使用可能包含用户输入的查询字符串,这是一种潜在的安全威胁。 默认情况下,ASP.NET 网页验证用户输入是否不包含脚本或 HTML 元素。 有关详细信息,请参阅脚本侵入概述

另请参阅

适用于