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

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

bool isTrue = Request.QueryString["value"] == "1";

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

You don't have to use either. A query string parameter does not have to have a value. You could just have a uri like this: http://someserver.com/somepage.aspx?SortById=&day=Monday

and then in your code:

if(Request.QueryString.AllKeys.Contains("SortById")) ...

Tags:

Asp.Net