.NET has [FromQuery] to get query parameters on the path.
For instance my/foo?bar=1 maps to:
public class MyController : Controller
{
[HttpGet]
public async Task<Whatever> Foo([FromQuery] int bar, ...
The problem is boolean parameters, which can be passed as ?bar=true but are often passed by presence alone. There's no standard enforcing this either way, but it's really common practice.
So this:
public class MyController : Controller
{
[HttpGet]
public async Task<Whatever> Foo([FromQuery] bool? bar, ...
Will have bar as true when my/foo?bar=true, but as null when my/foo?bar.
I need:
- Path
my/foo?barsetsbar = truein .NET - Path
my/foosetsbar = falseorbar = null(either is fine) in .NET - Ideally handle the other corner cases:
my/foo?bar=1setstruemy/foo?bar=0setsfalsemy/foo?bar=falsesetsfalse
I can write my own IModelBinder from scratch to handle this, but it seems like something that should be out of the box... MVC is 20 odd years old, this must be a commonly solved problem by now?
What's the best practice for these in .NET?
a commonly solved problem by nowyes, using custom binders. Just like passing "arrays", what you ask isn't standard and each framework (in the Django, Drupal sense, not Python/PHP/ASP.NET sense) used its own convention. And usingbar=1is a bad convention - integers aren't bools, and what is 2 or -1?true, some1, some mention using flags but some point out that a checkbox will always generate a query parameter, so what doesfooalone mean in that case?it seems like something that should be out of the boxwhy exactly would you expect that the ASP.NET Core devs should specifically cater for your weird and nonstandard method of handling parameters?