HttpUtility.ParseQueryString 方法

定義

解析查詢字串為 NameValueCollection

多載

名稱 Description
ParseQueryString(String)

利用編碼解析查詢字串。NameValueCollectionUTF8

ParseQueryString(String, Encoding)

利用指定的 Encoding解析查詢字串 NameValueCollection

ParseQueryString(String)

利用編碼解析查詢字串。NameValueCollectionUTF8

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

要解析的查詢字串。

傳回

查詢參數與值的 A 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)

利用指定的 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

傳回

查詢參數與值的 A NameValueCollection

例外狀況

querynull

-或-

encodingnull

備註

在回傳 NameValueCollection的 中,會解碼 URL 編碼的字元,並將同一查詢字串參數的多次出現列為單一條目,並以逗號分隔每個值。

Important

ParseQueryString 方法使用可能包含使用者輸入的查詢字串,這可能構成安全威脅。 預設情況下,ASP.NET 網頁會驗證使用者輸入中不包含腳本或 HTML 元素。 欲了解更多資訊,請參閱 腳本漏洞概述

另請參閱

適用於