HttpUtility.ParseQueryString 方法

定义

将查询字符串分析为 NameValueCollection.

重载

名称 说明
ParseQueryString(String)

将查询字符串分析为 NameValueCollection 使用 UTF8 编码。

ParseQueryString(String, Encoding)

使用指定的NameValueCollection查询字符串分析为Encoding查询字符串。

ParseQueryString(String)

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

将查询字符串分析为 NameValueCollection 使用 UTF8 编码。

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 编码字符进行解码,将同一查询字符串参数的多个匹配项列为一个条目,用逗号分隔每个值。

Important

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

另请参阅

适用于

ParseQueryString(String, Encoding)

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

使用指定的NameValueCollection查询字符串分析为Encoding查询字符串。

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 编码字符,将同一查询字符串参数的多个匹配项列为一个条目,其中逗号分隔每个值。

Important

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

另请参阅

适用于