HttpUtility.ParseQueryString 方法

定義

將查詢字串剖析到 NameValueCollection 之內。

多載

ParseQueryString(String)

使用 UTF8 編碼方式將查詢字串剖析成 NameValueCollection

ParseQueryString(String, Encoding)

使用指定的 Encoding 將查詢字串剖析成 NameValueCollection

ParseQueryString(String)

來源:
HttpUtility.cs
來源:
HttpUtility.cs
來源:
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 Web 網頁會驗證使用者輸入未包含指令碼或 HTML 項目。 如需詳細資訊,請參閱 Script Exploits Overview (指令碼攻擊概觀)。

另請參閱

適用於

ParseQueryString(String, Encoding)

來源:
HttpUtility.cs
來源:
HttpUtility.cs
來源:
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 Web 網頁會驗證使用者輸入未包含指令碼或 HTML 項目。 如需詳細資訊,請參閱 Script Exploits Overview (指令碼攻擊概觀)。

另請參閱

適用於