Skip to content

Commit e7b23fc

Browse files
authored
Updated HttpUtility ParseQueryString code snippets for C# and VB (#9342)
1 parent f8aab18 commit e7b23fc

File tree

5 files changed

+61
-121
lines changed

5 files changed

+61
-121
lines changed

snippets/csharp/VS_Snippets_WebNet/HttpUtility_ParseQueryString/cs/httputility_parsequerystring.aspx

Lines changed: 0 additions & 56 deletions
This file was deleted.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// <Snippet1>
2+
3+
using System;
4+
using System.Web;
5+
6+
class Program
7+
{
8+
static void Main()
9+
{
10+
// Parse the URL and get the query string
11+
var url = "https://www.microsoft.com?name=John&age=30&location=USA";
12+
var parsedUrl = url.Split('?')[1];
13+
14+
// The ParseQueryString method will parse the query string and return a NameValueCollection
15+
var paramsCollection = HttpUtility.ParseQueryString(parsedUrl);
16+
17+
// The foreach loop will iterate over the params collection and print the key and value for each param
18+
foreach (var key in paramsCollection.AllKeys)
19+
{
20+
Console.WriteLine($"Key: {key} => Value: {paramsCollection[key]}");
21+
}
22+
}
23+
}
24+
25+
// The example displays the following output:
26+
// Key: name => Value: John
27+
// Key: age => Value: 30
28+
// Key: location => Value: USA
29+
30+
// </Snippet1>

snippets/visualbasic/VS_Snippets_WebNet/HttpUtility_ParseQueryString/vb/httputility_parsequerystring.aspx

Lines changed: 0 additions & 61 deletions
This file was deleted.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
' <Snippet1>
2+
3+
Imports System.Collections.Specialized
4+
Imports System.Web
5+
6+
Public Class Sample
7+
Public Shared Sub Main()
8+
' Parse the URL and get the query string
9+
Dim url As String = "https://www.microsoft.com?name=John&age=30&location=USA"
10+
Dim parsedUrl As String = url.Split("?")(1)
11+
12+
' The ParseQueryString method will parse the query string and return a NameValueCollection
13+
Dim paramsCollection As NameValueCollection = HttpUtility.ParseQueryString(parsedUrl)
14+
15+
' The For Each loop will iterate over the params collection and print the key and value for each param
16+
For Each key As String In paramsCollection.AllKeys
17+
Console.WriteLine($"Key: {key} => Value: {paramsCollection(key)}")
18+
Next
19+
End Sub
20+
End Class
21+
22+
' The example displays the following output:
23+
' Key: name => Value: John
24+
' Key: age => Value: 30
25+
' Key: location => Value: USA
26+
27+
' </Snippet1>

xml/System.Web/HttpUtility.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -847,7 +847,7 @@
847847
<format type="text/markdown"><![CDATA[
848848
849849
## Remarks
850-
The <xref:System.Web.HttpUtility.ParseQueryString%2A> method uses <xref:System.Text.Encoding.UTF8%2A> format to parse the query string In the returned <xref:System.Collections.Specialized.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.
850+
The <xref:System.Web.HttpUtility.ParseQueryString%2A> method uses <xref:System.Text.Encoding.UTF8%2A> format to parse the query string In the returned <xref:System.Collections.Specialized.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.
851851
852852
> [!IMPORTANT]
853853
> The <xref:System.Web.HttpUtility.ParseQueryString%2A> method uses query strings that might contain user input, which is a potential security threat. By default, ASP.NET Web pages validate that user input does not include script or HTML elements. For more information, see [Script Exploits Overview](https://docs.microsoft.com/previous-versions/aspnet/w1sw53ds(v=vs.100)).
@@ -857,8 +857,8 @@
857857
## Examples
858858
The following code example demonstrates how to use the <xref:System.Web.HttpUtility.ParseQueryString%2A> method. Multiple occurrences of the same query string variable are consolidated in one entry of the returned <xref:System.Collections.Specialized.NameValueCollection>.
859859
860-
:::code language="aspx-csharp" source="~/snippets/csharp/VS_Snippets_WebNet/HttpUtility_ParseQueryString/cs/httputility_parsequerystring.aspx" id="Snippet1":::
861-
:::code language="aspx-vb" source="~/snippets/visualbasic/VS_Snippets_WebNet/HttpUtility_ParseQueryString/vb/httputility_parsequerystring.aspx" id="Snippet1":::
860+
:::code language="csharp" source="~/snippets/csharp/VS_Snippets_WebNet/HttpUtility_ParseQueryString/cs/httputility_parsequerystring.cs" interactive="try-dotnet" id="Snippet1":::
861+
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_WebNet/HttpUtility_ParseQueryString/vb/httputility_parsequerystring.vb" id="Snippet1":::
862862
863863
]]></format>
864864
</remarks>
@@ -922,7 +922,7 @@
922922
<format type="text/markdown"><![CDATA[
923923
924924
## Remarks
925-
In the returned <xref:System.Collections.Specialized.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.
925+
In the returned <xref:System.Collections.Specialized.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.
926926
927927
> [!IMPORTANT]
928928
> The <xref:System.Web.HttpUtility.ParseQueryString%2A> method uses query strings that might contain user input, which is a potential security threat. By default, ASP.NET Web pages validate that user input does not include script or HTML elements. For more information, see [Script Exploits Overview](https://docs.microsoft.com/previous-versions/aspnet/w1sw53ds(v=vs.100)).

0 commit comments

Comments
 (0)