Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use "true" or "1" for boolean querystring params

Tags:

asp.net

Does anyone use "true" or "false" when setting a query string param for a bool? or do most people use "1" or "0". I would be curious to know the consensus out there.

like image 338
PositiveGuy Avatar asked Feb 01 '10 16:02

PositiveGuy


People also ask

Can query Param be Boolean?

What. We now handle boolean query parameters in a more explicit way. For query parameters that are defined as a boolean type in our documentation, the case-insensitive strings true and 1 will be handled as a true value, and the case-insensitive strings false and 0 will be handled as the false value.

How do you pass Boolean value in RequestParam?

You can also use the primitive boolean type for @RequestParam in place of Boolean . In this case, Spring automatically assigns false . For example, the following isCanceled param is optional. When you hit the URI http://localhost:8080/orderStatus , then the Spring assigns false to the isCanceled primitive.

How do you pass a Boolean value to a string?

To convert Boolean to String in Java, use the toString() method. For this, firstly, we have declared two booleans. String str1 = new Boolean(bool1). toString(); String str2 = new Boolean(bool2).

What is QueryString parameter?

What are query string parameters? Query string parameters are extensions of a website's base Uniform Resource Locator (URL) loaded by a web browser or client application. Originally query strings were used to record the content of an HTML form or web form on a given page.


2 Answers

I do prefer "1/0", because it doesn't fall under localization requirements.

bool isTrue = Request.QueryString["value"] == "1"; 
like image 142
Rubens Farias Avatar answered Sep 22 '22 10:09

Rubens Farias


It may be worth mentioning that when using ASP.NET Core and binding via [FromQuery] you are forced to use boolean values as arguments.

[HttpGet("/api/foo")] public Task<NoContentResult> FooAction([FromQuery(Name = "bar")] bool isBar) { /*...*/ } 

This will work:

GET /api/foo?bar=true 

Passing an integer will result in an invalid ModelState returned by ASP.NET Core's ModelBinder

GET /api/foo?bar=1 
like image 21
B12Toaster Avatar answered Sep 21 '22 10:09

B12Toaster