@@ -73,45 +73,257 @@ Install-Package Skybrud.Essentials.AspNetCore -Version 1.0.0-alpha011
7373
7474### Extension methods for ` IQueryCollection `
7575
76+ #### Strings
77+
7678``` cshtml
7779@using Microsoft.AspNetCore.Http
7880@using Microsoft.Extensions.Primitives
7981@using Skybrud.Essentials.AspNetCore
82+ @{
83+
84+ IQueryCollection query = new QueryCollection(new Dictionary<string, StringValues> {
85+ {"id", new StringValues("1")},
86+ {"ids", new StringValues(new []{"1", "2", "3"})},
87+ {"moreIds", new StringValues(new []{"5,6", "7", "8"})}
88+ });
89+
90+ // Prints "1"
91+ string? id = query.GetString("id");
92+ <pre>@id</pre>
93+
94+ // Prints "1,2,3"
95+ string[] ids = query.GetStringArray("ids");
96+ <pre>@string.Join(",", ids)</pre>
97+
98+ // Prints "5,6,7,8"
99+ List<string> moreIds = query.GetStringList("moreIds");
100+ <pre>@string.Join(",", moreIds)</pre>
101+
102+ // Prints "'nope' not found"
103+ if (query.TryGetString("nope", out string? nope)) {
104+ <pre>@nope</pre>
105+ } else {
106+ <pre>'nope' not found.</pre>
107+ }
108+
109+ }
110+ ```
80111
112+ #### Int32
113+
114+ ``` cshtml
115+ @using Microsoft.AspNetCore.Http
116+ @using Microsoft.Extensions.Primitives
117+ @using Skybrud.Essentials.AspNetCore
81118@{
82119
83- IQueryCollection query1 = new QueryCollection(new Dictionary<string, StringValues> {
120+ IQueryCollection query = new QueryCollection(new Dictionary<string, StringValues> {
84121 {"id", new StringValues("1")},
85122 {"ids", new StringValues(new []{"1", "2", "3"})},
86- {"piAsFloat", "3.14"},
87- {"piAsDouble", "3.1415926535"},
88- {"flag1", ""},
89- {"flag2", "true"}
123+ {"moreIds", new StringValues(new []{"5,6", "7", "8"})}
90124 });
91-
92- int idInt = query1.GetInt32("id");
93- long idLong = query1.GetInt64("id");
94-
95- int[] idsInt = query1.GetInt32Array("ids");
96- long[] idsLong = query1.GetInt64Array("ids");
97125
98- float piFloat = query1.GetFloat("piAsFloat");
99- double piDouble = query1.GetDouble("piAsDouble");
100-
101- bool flag1 = query1.GetBoolean("flag1", true);
102- bool flag2 = query1.GetBoolean("flag2", true);
103-
104- <pre>ID (int): @idInt</pre>
105- <pre>ID (long): @idLong</pre>
106-
107- <pre>IDs (int): @string.Join(", ", idsInt)</pre>
108- <pre>IDs (long): @string.Join(", ", idsLong)</pre>
126+ // Prints "1"
127+ int id = query.GetInt32("id");
128+ <pre>@id</pre>
109129
110- <pre>PI (float): @piFloat</pre>
111- <pre>PI (double): @piDouble</pre>
112-
113- <pre>Flag1: @flag1</pre>
114- <pre>Flag1: @flag2</pre>
130+ // Prints "2" (via fallback)
131+ int id2 = query.GetInt32("id2", 2);
132+ <pre>@id2</pre>
133+
134+ // Prints "" (since null is rendered as empty)
135+ int? id3 = query.GetInt32OrNull("id3");
136+ <pre>@id3</pre>
137+
138+ // Prints "1,2,3"
139+ int[] ids = query.GetInt32Array("ids");
140+ <pre>@string.Join(",", ids)</pre>
141+
142+ // Prints "5,6,7,8"
143+ List<int> moreIds = query.GetInt32List("moreIds");
144+ <pre>@string.Join(",", moreIds)</pre>
145+
146+ // Prints "'nope' not found"
147+ if (query.TryGetInt32("nope", out int? nope)) {
148+ <pre>@nope</pre>
149+ } else {
150+ <pre>'nope' not found.</pre>
151+ }
152+
153+ }
154+ ```
155+
156+ #### Int64
157+
158+ ``` cshtml
159+ @using Microsoft.AspNetCore.Http
160+ @using Microsoft.Extensions.Primitives
161+ @using Skybrud.Essentials.AspNetCore
162+ @{
163+
164+ IQueryCollection query = new QueryCollection(new Dictionary<string, StringValues> {
165+ {"id", new StringValues("1")},
166+ {"ids", new StringValues(new []{"1", "2", "3"})},
167+ {"moreIds", new StringValues(new []{"5,6", "7", "8"})}
168+ });
169+
170+ // Prints "1"
171+ long id = query.GetInt64("id");
172+ <pre>@id</pre>
173+
174+ // Prints "2" (via fallback)
175+ long id2 = query.GetInt64("id2", 2);
176+ <pre>@id2</pre>
177+
178+ // Prints "" (since null is rendered as empty)
179+ long? id3 = query.GetInt64OrNull("id3");
180+ <pre>@id3</pre>
181+
182+ // Prints "1,2,3"
183+ long[] ids = query.GetInt64Array("ids");
184+ <pre>@string.Join(",", ids)</pre>
185+
186+ // Prints "5,6,7,8"
187+ List<long> moreIds = query.GetInt64List("moreIds");
188+ <pre>@string.Join(",", moreIds)</pre>
189+
190+ // Prints "'nope' not found"
191+ if (query.TryGetInt64("nope", out long? nope)) {
192+ <pre>@nope</pre>
193+ } else {
194+ <pre>'nope' not found.</pre>
195+ }
196+
197+ }
198+ ```
199+
200+ #### Float
201+
202+ ``` cshtml
203+ @using Microsoft.AspNetCore.Http
204+ @using Microsoft.Extensions.Primitives
205+ @using Skybrud.Essentials.AspNetCore
206+ @{
207+
208+ IQueryCollection query = new QueryCollection(new Dictionary<string, StringValues> {
209+ {"pi", new StringValues("3.14")},
210+ {"values", new StringValues(new []{"3.14", "6.28", "9.42"})},
211+ {"otherValues", new StringValues(new []{"3.14,6.28", "9.42"})}
212+ });
213+
214+ // Prints "3.14"
215+ float pi = query.GetFloat("pi");
216+ <pre>@pi</pre>
217+
218+ // Prints "1.23" (via fallback)
219+ float meh = query.GetFloat("meh", 1.23f);
220+ <pre>@meh</pre>
221+
222+ // Prints "" (since null is rendered as empty)
223+ float? meh2 = query.GetFloatOrNull("meh");
224+ <pre>@meh2</pre>
225+
226+ // Prints "3.14,6.28,9.42"
227+ float[] values = query.GetFloatArray("values");
228+ <pre>@string.Join(",", values)</pre>
229+
230+ // Prints "3.14,6.28,9.42"
231+ List<float> otherValues = query.GetFloatList("otherValues");
232+ <pre>@string.Join(",", otherValues)</pre>
233+
234+ // Prints "'nope' not found"
235+ if (query.TryGetFloat("nope", out float? nope)) {
236+ <pre>@nope</pre>
237+ } else {
238+ <pre>'nope' not found.</pre>
239+ }
240+
241+ }
242+ ```
243+
244+ #### Double
245+
246+ ``` cshtml
247+ @using Microsoft.AspNetCore.Http
248+ @using Microsoft.Extensions.Primitives
249+ @using Skybrud.Essentials.AspNetCore
250+ @{
251+
252+ IQueryCollection query = new QueryCollection(new Dictionary<string, StringValues> {
253+ {"pi", new StringValues("3.1415926535")},
254+ {"values", new StringValues(new []{"3.1415926535", "6.283185307", "9.4247779605"})},
255+ {"otherValues", new StringValues(new []{"3.1415926535,6.283185307", "9.4247779605"})}
256+ });
257+
258+ // Prints "3.1415926535"
259+ double pi = query.GetDouble("pi");
260+ <pre>@pi</pre>
261+
262+ // Prints "1.23" (via fallback)
263+ double meh = query.GetDouble("meh", 1.23);
264+ <pre>@meh</pre>
265+
266+ // Prints "" (since null is rendered as empty)
267+ double? meh2 = query.GetDoubleOrNull("meh");
268+ <pre>@meh2</pre>
269+
270+ // Prints "3.1415926535,6.283185307,9.4247779605"
271+ double[] values = query.GetDoubleArray("values");
272+ <pre>@string.Join(",", values)</pre>
273+
274+ // Prints "3.1415926535,6.283185307,9.4247779605"
275+ List<double> otherValues = query.GetDoubleList("otherValues");
276+ <pre>@string.Join(",", otherValues)</pre>
277+
278+ // Prints "'nope' not found"
279+ if (query.TryGetDouble("nope", out double? nope)) {
280+ <pre>@nope</pre>
281+ } else {
282+ <pre>'nope' not found.</pre>
283+ }
284+
285+ }
286+ ```
287+
288+ #### Boolean
289+
290+ ``` cshtml
291+ @using Microsoft.AspNetCore.Http
292+ @using Microsoft.Extensions.Primitives
293+ @using Skybrud.Essentials.AspNetCore
294+ @{
295+
296+ IQueryCollection query = new QueryCollection(new Dictionary<string, StringValues> {
297+ {"a", new StringValues("true")},
298+ {"b", new StringValues("1")}
299+ });
300+
301+ // Prints "True"
302+ bool a = query.GetBoolean("a");
303+ <pre>@a</pre>
304+
305+ // Prints "True"
306+ bool b = query.GetBoolean("b");
307+ <pre>@b</pre>
308+
309+ // Prints "False"
310+ bool c = query.GetBoolean("c");
311+ <pre>@c</pre>
312+
313+ // Prints "True" (via fallback)
314+ bool d = query.GetBoolean("d", true);
315+ <pre>@d</pre>
316+
317+ // Prints "" (since null is rendered as empty)
318+ bool? e = query.GetBooleanOrNull("e");
319+ <pre>@e</pre>
320+
321+ // Prints "'nope' not found"
322+ if (query.TryGetBoolean("nope", out bool? nope)) {
323+ <pre>@nope</pre>
324+ } else {
325+ <pre>'nope' not found.</pre>
326+ }
115327
116328}
117329```
0 commit comments