Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I bind list of guids on the query string in C# web api?

How do I bind a list of Guids passed on the query string for a web-api call.

I've tried comma separated list, an array, etc. but they always come up with an empty guid on the controller action

public endPoint:

public ApiResult Get([FromUri]List<Guid> listIds)

What I've tried

endpoint?listIds=78CC5308-5A3D-40C2-8D7C-3AAB0CAC618B,6DA7AFB8-D862-4861-8EA2-EE5EB9BC7C6A

endpoint?listIds=["78CC5308-5A3D-40C2-8D7C-3AAB0CAC618B","6DA7AFB8-D862-4861-8EA2-EE5EB9BC7C6A"]

All I get each time I try is a list with one element and it is the empty GUID

like image 912
MasterCrafter Avatar asked Oct 19 '25 03:10

MasterCrafter


2 Answers

The right way for webapi is using the query parameter multiple times,no matter the type.

i.e:

endpoint?listIds=1234-5678&listIds=5555-4444....
like image 199
Ziv Weissman Avatar answered Oct 21 '25 17:10

Ziv Weissman


The trick is to add an individual entry for each guid in the query string. So the values bind properly when I GET

endpoint?listIds=78CC5308-5A3D-40C2-8D7C-3AAB0CAC618B&listIds=6DA7AFB8-D862-4861-8EA2-EE5EB9BC7C6A
like image 26
MasterCrafter Avatar answered Oct 21 '25 19:10

MasterCrafter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!