✅ Binding array with default value doesn't work
I've got the following model and controller
The
https://localhost:5001/test?Test=3&Sections=USER
resolves to {"test":3,"sections":[]}
, but if I remove default value for a Sections
property it resolves to {"test":3,"sections":["USER"]}
. Why behavior is different from int default value?28 Replies
just guessing here:
seems the way how its instance created will be filled with the correct data, but afterwars the property initializer is run, resulting in an overrriden value.
can u test the behavior with
required
for Sections
instead of giving a default value?"property initializer is run, resulting in an overrriden value." - but this doesn't apply to nullable int
which is also a reference type
indeed, its weird 😒
just tested same with string instead of int
and it works, but arrays or rather collections seems to behave oddly
hmm, tbh im clueless now as well 🤔
if u expand the auto property to set a break point on the setter, u should be able to see whats going on, and maybe the stack trace info helps figuring out a solution, maybe? 😂
funny.
GET https://localhost:5001/test?Sections=TEST
and same but without a default value for backing property
the f... o.O
i doubt its a bug in the model binding stuff.. but thats the only rason i can think of D:
can u show the stacktrace of both variants?
stacktrace won't help actually tbh
project is clean
literally only Add and Map controllers
well, i dont want to see the parts of ur code, but where the model is created and how its filled by the framework ;p
worst case i would file an issue in the repo, maybe it really is a bug
but i would assume that response time there is quite long
which dotnet version btw?
(and asp.net version)
.net 7
same on behavior on 6
i gonna smoke and think, but im most likely out of ideas 😒
only ugly workarounds come to mind =/
Probably because it can't figure out how to put a single string in an array, but when it tries to make its own list, it makes a
List<string>
(and puts the value in)
Either way why are you trying to fill a list/array from the parameters, if you can only put one value for the parameter in the URL?There can be a several params:
/test?Sections=TEST&Sections=TEST2
Web API parameter binding is not able to convert several parameters from the query string into an arrayhttps://stackoverflow.com/questions/30318428/multiple-parameters-by-the-same-name
Stack Overflow
Multiple parameters by the same name
Consider the following ApiController:
public class SomeController : ApiController
{
[HttpGet]
public class SomeFunction(int someVal = 0) {
...
}
}
This works as expected:
htt...
new List
intead of array seems to be workingi mean, array size cant be changed
array.empty is length 0, it tries to put in the string, fails, so empty array
Yeah but, couldn't it just create a new array?
I just was trying to be efficient and used empty array everywhere
Guess I'll have to make a change then
It probably calls icollection.add or something like that, I doubt it implements proper inserting for each type of colleciton
you could try
Enumerable.Empty
Exception
Well, premature optimization is the root of evil
I tried to be efficient and got backstabbed
weird that asp doesn't throw in case of array
like it does with enumerable empty
for what code u got that exception?
Changed Array.Empty to Enumerable.Empty
aah
also i dont think thats premature optimization, its simply a well defined model imo
i guess the question is answered now, right? so please $close it if thats the case
Use the
/close
command to mark a forum thread as answeredGuess yes...