dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.45k stars 10.03k forks source link

Allow to parse 1 to boolean for SupplyParameterFromQuery attribute in Blazor app #50726

Open MarvinKlein1508 opened 1 year ago

MarvinKlein1508 commented 1 year ago

Is there an existing issue for this?

Is your feature request related to a problem? Please describe the problem.

I have a parameter which is defined as boolean

[Parameter, SupplyParameterFromQuery]
public bool Copy { get; set; }

However I get this exception when supplying 1 for the parameter

System.InvalidOperationException: "Cannot parse the value '1' as type 'System.Boolean' for 'Copy'."

Describe the solution you'd like

It is fairly common to use 1 for boolean true and 0 for false. So IMO it should work out of the box to supply 0 and 1 for boolean parameters.

Additional context

No response

javiercn commented 1 year ago

@MarvinKlein1508 thanks for contacting us.

I don't think anything on the stack supports this behavior (not Blazor and not MVC) and this is the first time we've received such ask.

MarvinKlein1508 commented 1 year ago

@javiercn there is this question on StackOverFlow with over 60k views. https://stackoverflow.com/questions/2178103/use-true-or-1-for-boolean-querystring-params

javiercn commented 1 year ago

@MarvinKlein1508 sure, it's a 13-year-old issue.

If this hasn't driven more asks it's likely that it can easily be accomplish in other ways and not a priority and that the view count doesn't accurately reflect the actual interest. It's very easy to land on a page to realize it's not what you are looking for.

MarvinKlein1508 commented 1 year ago

@javiercn Yes but you can see on the amount of upvotes that many people were interested in this in some way or another.

I'Ve just thought about it. Isn't this a usecase to override the SetParametersAsync method in a blazor app?

gragra33 commented 1 year ago

@MarvinKlein1508 QueryString is just that, string key and value - you can't push values to the wrong type, you need to manage it. So, properties have getters and setters, so you can convert the string? value to boolean? and back. Something like:

private bool? _copy;

[Parameter, SupplyParameterFromQuery]
public string? Copy
{
    get => _copy is null ? "" : _copy == true ? "1" : "0";
    set => _copy = value is null ? null : value == "1";
}
MarvinKlein1508 commented 1 year ago

@gragra33 yes but I don't want to parse my Copy property then evertime I use it.

gragra33 commented 1 year ago

@MarvinKlein1508 There are no other options or converters. SupplyParameterFromQuery is sealed, so you can't write a wrapper to hide the implementation. I've had a quick look and Source Generators are used, so would be involved to make changes.

Either store as 'string' and apply logic when required or as above.

ghost commented 1 year ago

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

ghost commented 11 months ago

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

schmellow commented 11 months ago

It probably would help if the behavior around parameter/property conversion was documented somehow? So SupplyParameterFromQuery at least works automagically on numbers and dates (but i assume only for ISO format for the latter), but can blow up on booleans. Simple example: <form> (get method by default) + checkbox inside that form. Checkbox value gets translated as "?checkbox=on" on submit, which does not parse. But then if you go interactive with onsubmit handler and just bind that checkbox to property directly without the attribute, it just works. Feels inconsistent and confusing

MarvinKlein1508 commented 11 months ago

@schmellow good point. This could also be interesting for SSR forms which submits forms via GET.