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 メソッドの使用方法を示します。 同じクエリ文字列変数が複数出現すると、返される の 1 つのエントリに統合されます 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 エンコードされた文字がデコードされ、同じクエリ文字列パラメーターが複数出現し、各値をコンマで区切った 1 つのエントリとして一覧表示されます。

重要

メソッドは ParseQueryString 、潜在的なセキュリティ上の脅威であるユーザー入力を含む可能性のあるクエリ文字列を使用します。 既定では、ASP.NET Web ページによって、ユーザー入力にスクリプトまたは 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 でエンコードされた文字がデコードされ、同じクエリ文字列パラメーターが複数出現し、各値がコンマで区切られた 1 つのエントリとして一覧表示されます。

重要

メソッドは ParseQueryString 、潜在的なセキュリティ上の脅威であるユーザー入力を含む可能性のあるクエリ文字列を使用します。 既定では、ASP.NET Web ページによって、ユーザー入力にスクリプトまたは HTML 要素が含まれていないかどうかが検証されます。 詳細については、「スクリプトによる攻略の概要」を参照してください。

こちらもご覧ください

適用対象