How to pass list of complex types in query string?
-
12-11-2019 - |
Вопрос
How would I pass a list of complex types in ServiceStack? For example my Request DTO looks like this:
//Request DTO
public class Test
{
public IList<Fund> Funds { get; set; }
}
public class Fund
{
public string Key { get; set; }
public int Percent { get; set; }
}
How can I pass the serialized object via HTTP get?
http://localhost:49490/api/funds={ ?? }
KeyValueDataContractDeserializer: Error converting to type: Type definitions should start with a '{', expecting serialized type 'Fund', got string starting with: asdf
Решение
ServiceStack parses the queryString using the JSV Format this is basically JSON with the CSV-style quotes (i.e. only needs quotes when your value has an escape char).
Although you haven't defined a Custom Route here, in most cases your custom route is the same as your Request DTO which in this case is Test not /funds.
So assuming a custom route looks like:
Routes.Add<Test>("/test");
You can call your service via a QueryString like:
http://localhost:49490/api/test?Funds=[{Key:Key1,Percent:1},{Key:Key2,Percent:2}]
On a side note Interfaces on DTOs are generally a bad idea, you should consider avoiding (at least limiting) its use at all times.