From cc6c37986c267e1e689023c0514195f49ef87b69 Mon Sep 17 00:00:00 2001 From: Joan Gil Date: Thu, 28 Sep 2023 18:16:44 +0200 Subject: [PATCH 1/3] Updated HttpUtility ParseQueryString code snippets --- .../cs/httputility_parsequerystring.aspx | 56 ----------------- .../cs/httputility_parsequerystring.cs | 23 +++++++ .../vb/httputility_parsequerystring.aspx | 61 ------------------- .../vb/httputility_parsequerystring.vb | 20 ++++++ xml/System.Web/HttpUtility.xml | 8 +-- 5 files changed, 47 insertions(+), 121 deletions(-) delete mode 100644 snippets/csharp/VS_Snippets_WebNet/HttpUtility_ParseQueryString/cs/httputility_parsequerystring.aspx create mode 100644 snippets/csharp/VS_Snippets_WebNet/HttpUtility_ParseQueryString/cs/httputility_parsequerystring.cs delete mode 100644 snippets/visualbasic/VS_Snippets_WebNet/HttpUtility_ParseQueryString/vb/httputility_parsequerystring.aspx create mode 100644 snippets/visualbasic/VS_Snippets_WebNet/HttpUtility_ParseQueryString/vb/httputility_parsequerystring.vb diff --git a/snippets/csharp/VS_Snippets_WebNet/HttpUtility_ParseQueryString/cs/httputility_parsequerystring.aspx b/snippets/csharp/VS_Snippets_WebNet/HttpUtility_ParseQueryString/cs/httputility_parsequerystring.aspx deleted file mode 100644 index 6556b54acdc..00000000000 --- a/snippets/csharp/VS_Snippets_WebNet/HttpUtility_ParseQueryString/cs/httputility_parsequerystring.aspx +++ /dev/null @@ -1,56 +0,0 @@ - -<%@ Page Language="C#"%> - - - - - - - HttpUtility ParseQueryString Example - - -
- Query string variables are: - - - - - diff --git a/snippets/csharp/VS_Snippets_WebNet/HttpUtility_ParseQueryString/cs/httputility_parsequerystring.cs b/snippets/csharp/VS_Snippets_WebNet/HttpUtility_ParseQueryString/cs/httputility_parsequerystring.cs new file mode 100644 index 00000000000..adb8c3d5713 --- /dev/null +++ b/snippets/csharp/VS_Snippets_WebNet/HttpUtility_ParseQueryString/cs/httputility_parsequerystring.cs @@ -0,0 +1,23 @@ +// + +using System.Web; + +// 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 for each 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 + +// \ No newline at end of file diff --git a/snippets/visualbasic/VS_Snippets_WebNet/HttpUtility_ParseQueryString/vb/httputility_parsequerystring.aspx b/snippets/visualbasic/VS_Snippets_WebNet/HttpUtility_ParseQueryString/vb/httputility_parsequerystring.aspx deleted file mode 100644 index fd6ecaf101a..00000000000 --- a/snippets/visualbasic/VS_Snippets_WebNet/HttpUtility_ParseQueryString/vb/httputility_parsequerystring.aspx +++ /dev/null @@ -1,61 +0,0 @@ - -<%@ Page Language="VB" %> - - - - - - - HttpUtility ParseQueryString Example - - -
- Query string variables are: - - - - - diff --git a/snippets/visualbasic/VS_Snippets_WebNet/HttpUtility_ParseQueryString/vb/httputility_parsequerystring.vb b/snippets/visualbasic/VS_Snippets_WebNet/HttpUtility_ParseQueryString/vb/httputility_parsequerystring.vb new file mode 100644 index 00000000000..c577a0d6724 --- /dev/null +++ b/snippets/visualbasic/VS_Snippets_WebNet/HttpUtility_ParseQueryString/vb/httputility_parsequerystring.vb @@ -0,0 +1,20 @@ +' + +' 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 + +' The example displays the following output: +' Key: name => Value: John +' Key: age => Value: 30 +' Key: location => Value: USA + +' \ No newline at end of file diff --git a/xml/System.Web/HttpUtility.xml b/xml/System.Web/HttpUtility.xml index f0a70182299..527850b3e77 100644 --- a/xml/System.Web/HttpUtility.xml +++ b/xml/System.Web/HttpUtility.xml @@ -847,7 +847,7 @@ method uses format to parse the query string In the returned , 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. + The method uses format to parse the query string In the returned , 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. > [!IMPORTANT] > The 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 @@ ## Examples The following code example demonstrates how to use the method. Multiple occurrences of the same query string variable are consolidated in one entry of the returned . - :::code language="aspx-csharp" source="~/snippets/csharp/VS_Snippets_WebNet/HttpUtility_ParseQueryString/cs/httputility_parsequerystring.aspx" id="Snippet1"::: - :::code language="aspx-vb" source="~/snippets/visualbasic/VS_Snippets_WebNet/HttpUtility_ParseQueryString/vb/httputility_parsequerystring.aspx" id="Snippet1"::: + :::code language="csharp" source="~/snippets/csharp/VS_Snippets_WebNet/HttpUtility_ParseQueryString/cs/httputility_parsequerystring.cs" interactive="try-dotnet" id="Snippet1"::: + :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_WebNet/HttpUtility_ParseQueryString/vb/httputility_parsequerystring.vb" id="Snippet1"::: ]]> @@ -922,7 +922,7 @@ , 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. + In the returned , 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. > [!IMPORTANT] > The 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)). From 61727aebb1cf75bf28796a0476e72da9177aa23a Mon Sep 17 00:00:00 2001 From: Joan Gil Date: Wed, 8 Nov 2023 09:59:13 +0100 Subject: [PATCH 2/3] Update c# snippet to wrap it inside a Program class --- .../cs/httputility_parsequerystring.cs | 25 ++++++++++++------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/snippets/csharp/VS_Snippets_WebNet/HttpUtility_ParseQueryString/cs/httputility_parsequerystring.cs b/snippets/csharp/VS_Snippets_WebNet/HttpUtility_ParseQueryString/cs/httputility_parsequerystring.cs index adb8c3d5713..8c928a161a4 100644 --- a/snippets/csharp/VS_Snippets_WebNet/HttpUtility_ParseQueryString/cs/httputility_parsequerystring.cs +++ b/snippets/csharp/VS_Snippets_WebNet/HttpUtility_ParseQueryString/cs/httputility_parsequerystring.cs @@ -1,18 +1,25 @@ // +using System; using System.Web; -// 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]; +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 ParseQueryString method will parse the query string and return a NameValueCollection + var paramsCollection = HttpUtility.ParseQueryString(parsedUrl); -// The for each 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 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: From d4f906e51aaf428eead78e85d94a159b9e3b59ca Mon Sep 17 00:00:00 2001 From: Joan Gil Date: Wed, 8 Nov 2023 12:35:57 +0100 Subject: [PATCH 3/3] Update vb snippet to wrap it inside a Sample and Main class --- .../vb/httputility_parsequerystring.vb | 25 ++++++++++++------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/snippets/visualbasic/VS_Snippets_WebNet/HttpUtility_ParseQueryString/vb/httputility_parsequerystring.vb b/snippets/visualbasic/VS_Snippets_WebNet/HttpUtility_ParseQueryString/vb/httputility_parsequerystring.vb index c577a0d6724..528c4df0ae3 100644 --- a/snippets/visualbasic/VS_Snippets_WebNet/HttpUtility_ParseQueryString/vb/httputility_parsequerystring.vb +++ b/snippets/visualbasic/VS_Snippets_WebNet/HttpUtility_ParseQueryString/vb/httputility_parsequerystring.vb @@ -1,16 +1,23 @@ ' -' 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) +Imports System.Collections.Specialized +Imports System.Web -' The ParseQueryString method will parse the query string and return a NameValueCollection -Dim paramsCollection As NameValueCollection = HttpUtility.ParseQueryString(parsedUrl) +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 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 + ' 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