HttpUtility.ParseQueryString HttpUtility.ParseQueryString HttpUtility.ParseQueryString HttpUtility.ParseQueryString Method

定義

將查詢字串剖析為 NameValueCollection Parses a query string into a NameValueCollection.

多載

ParseQueryString(String) ParseQueryString(String) ParseQueryString(String) ParseQueryString(String)

使用 NameValueCollection 編碼,將查詢字串剖析為 UTF8 Parses a query string into a NameValueCollection using UTF8 encoding.

ParseQueryString(String, Encoding) ParseQueryString(String, Encoding) ParseQueryString(String, Encoding)

使用指定的 NameValueCollection,將查詢字串剖析為 Encoding Parses a query string into a NameValueCollection using the specified Encoding.

ParseQueryString(String) ParseQueryString(String) ParseQueryString(String) ParseQueryString(String)

使用 NameValueCollection 編碼,將查詢字串剖析為 UTF8 Parses a query string into a NameValueCollection using UTF8 encoding.

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 String String String

要剖析的查詢字串。 The query string to parse.

傳回

由查詢參數和值所組成的 NameValueCollection A NameValueCollection of query parameters and values.

例外狀況

範例

下列程式碼範例會示範如何使用 ParseQueryString 方法。The following code example demonstrates how to use the ParseQueryString method. 相同的查詢字串變數的多個項目將會傳回一個項目中合併NameValueCollectionMultiple occurrences of the same query string variable are consolidated in one entry of the returned NameValueCollection.

<%@ Page Language="C#"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

  protected void Page_Load(object sender, EventArgs e)
  {
    String currurl = HttpContext.Current.Request.RawUrl;
    String querystring = null ;

    // Check to make sure some query string variables
    // exist and if not add some and redirect.
    int iqs = currurl.IndexOf('?');
    if (iqs == -1)
    {
      String redirecturl = currurl + "?var1=1&var2=2+2%2f3&var1=3";
      Response.Redirect(redirecturl, true); 
    }
    // If query string variables exist, put them in
    // a string.
    else if (iqs >= 0)
    {
      querystring = (iqs < currurl.Length - 1) ? currurl.Substring(iqs + 1) : String.Empty;
    }
   
    // Parse the query string variables into a NameValueCollection.
    NameValueCollection qscoll = HttpUtility.ParseQueryString(querystring);
    
    // Iterate through the collection.
    StringBuilder sb = new StringBuilder("<br />");
    foreach (String s in qscoll.AllKeys)
    {
      sb.Append(s + " - " + qscoll[s] + "<br />");
    }
    
    // Write the result to a label.
    ParseOutput.Text = sb.ToString();

  }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>HttpUtility ParseQueryString Example</title>
</head>
<body>
    <form id="form1" runat="server">
      Query string variables are:
      <asp:Label  id="ParseOutput"
                  runat="server" />
    </form>
</body>
</html>
<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)

    Dim currurl As String = HttpContext.Current.Request.RawUrl
    Dim querystring As String = Nothing
    
    ' Check to make sure some query string variables
    ' exist and if not add some and redirect.
    Dim iqs As Int32 = currurl.IndexOf("?".ToCharArray())
    If (iqs = -1) Then
      
      Dim redirecturl As String = currurl & "?var1=1&var2=2+2%2f3&var1=3"
      Response.Redirect(redirecturl, True)
      
      ' If query string variables exist, put them in
      ' a string.
    ElseIf (iqs >= 0) Then
      
      If (iqs < currurl.Length - 1) Then
        querystring = currurl.Substring(iqs + 1)
      End If
          
    End If

    ' Parse the query string variables into a NameValueCollection.
    Dim qscoll As NameValueCollection = HttpUtility.ParseQueryString(querystring)
    
    ' Iterate through the collection.
    Dim sb As New StringBuilder("<br />")
    For Each s As String In qscoll.AllKeys
      
      sb.Append(s & " - " & qscoll(s) & "<br />")
    
    Next s
    
    ' Write the result to a label
    ParseOutput.Text = sb.ToString()
    
  End Sub
  
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>HttpUtility ParseQueryString Example</title>
</head>
<body>
    <form id="Form1" runat="server">
      Query string variables are:
      <asp:Label  id="ParseOutput"
                  runat="server" />
    </form>
</body>
</html>

備註

ParseQueryString方法會使用UTF8格式來剖析查詢字串中傳回NameValueCollection、 解碼 URL 編碼的字元,以及多次出現相同的查詢字串參數的以逗號分隔每個列示為單一項目值。The ParseQueryString method uses UTF8 format to parse the query string In the returned NameValueCollection, URL-encoded characters are decoded and multiple occurrences of the same query string parameter are listed as a single entry with a comma separating each value.

重要

ParseQueryString方法會使用查詢字串,其中可能包含使用者輸入,也就是潛在的安全性威脅。The ParseQueryString method uses query strings that might contain user input, which is a potential security threat. 根據預設,ASP.NET Web 網頁會驗證使用者輸入未包含指令碼或 HTML 項目。By default, ASP.NET Web pages validate that user input does not include script or HTML elements. 如需詳細資訊,請參閱 Script Exploits Overview (指令碼攻擊概觀)。For more information, see Script Exploits Overview.

另請參閱

ParseQueryString(String, Encoding) ParseQueryString(String, Encoding) ParseQueryString(String, Encoding)

使用指定的 NameValueCollection,將查詢字串剖析為 Encoding Parses a query string into a NameValueCollection using the specified 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
參數
query
String String String String

要剖析的查詢字串。 The query string to parse.

encoding
Encoding Encoding Encoding Encoding

要使用的 Encoding The Encoding to use.

傳回

由查詢參數和值所組成的 NameValueCollection A NameValueCollection of query parameters and values.

例外狀況

querynull query is null. -或- - or - encodingnull encoding is null.

備註

在傳回NameValueCollection、 解碼 URL 編碼的字元和相同的查詢字串參數的多個項目會以單一項目列出以逗號分隔每個值。In the returned NameValueCollection, URL-encoded characters are decoded and multiple occurrences of the same query string parameter are listed as a single entry with a comma separating each value.

重要

ParseQueryString方法會使用查詢字串,其中可能包含使用者輸入,也就是潛在的安全性威脅。The ParseQueryString method uses query strings that might contain user input, which is a potential security threat. 根據預設,ASP.NET Web 網頁會驗證使用者輸入未包含指令碼或 HTML 項目。By default, ASP.NET Web pages validate that user input does not include script or HTML elements. 如需詳細資訊,請參閱 Script Exploits Overview (指令碼攻擊概觀)。For more information, see Script Exploits Overview.

另請參閱

適用於