Is it possible to pass a complex type as a parameter to a Web API mehtod? I would like to pass an ArrayList to a method. Is that possible or should I go back to WCF? Thanks.
You would never do that. You would serialize the array into a JSON collection and do a POST with the JSON data as the message body.
This is quite easy using the new HttpClient class, which you can use from Winforms apps (part of .NET 4.5 and as part of Web API).
Just to clarify things in my head. I have a method in my Web API that accepts 3 parameters (Int, string, ArrayList). I would need to create custom model binder to map data to parameters. Then if I want to call it from a Winforms app, I should serialize
the ArrayList (and the other parameters?) in JSON or use XML and then do POST witht the JSON/XML in the message body. Is that correct? Do you have any examples that I could look at to help me get going? Thanks for you help.
There's no need to use a custom model binder in most cases, but it's an option if you need to do something special.
Don't use the ArrayList type on the method signature. You can use a strongly typed collection or if you dont want to create a class for the data you can use the handy JsonValue type.
Here's an example of how you can parse an array of data using both the methods I described:
public class TestController : ApiController
{
public int Post(List<DataClass> data)
{
return data.Count;
}
public int Put(JsonValue value)
{
return value.Count;
//return data.Count;
}
}
public class DataClass
{
public string Name { get; set; }
public int Age { get; set; }
}
As for the Winforms app, you would have to use something like the WebRequest class since you can't use the new HttpClient. I'm sure there are plenty of examples on how to do that on the web :)
jfreemanSTR
Member
6 Points
4 Posts
Complex Type Parameter
Mar 07, 2012 01:23 PM|LINK
Is it possible to pass a complex type as a parameter to a Web API mehtod? I would like to pass an ArrayList to a method. Is that possible or should I go back to WCF? Thanks.
Jonathan
SiggiGG
Member
265 Points
105 Posts
Re: Complex Type Parameter
Mar 07, 2012 01:38 PM|LINK
Can't see why not, you can even write your own custom model binder to map data to parameters.
jfreemanSTR
Member
6 Points
4 Posts
Re: Complex Type Parameter
Mar 07, 2012 01:41 PM|LINK
Does anyone have an example of something like this? I'd like to call the Web API from a WinForms app. Not sure how to pass an ArrayList in a URL.
SiggiGG
Member
265 Points
105 Posts
Re: Complex Type Parameter
Mar 07, 2012 01:42 PM|LINK
You would never do that. You would serialize the array into a JSON collection and do a POST with the JSON data as the message body.
This is quite easy using the new HttpClient class, which you can use from Winforms apps (part of .NET 4.5 and as part of Web API).
jfreemanSTR
Member
6 Points
4 Posts
Re: Complex Type Parameter
Mar 07, 2012 01:44 PM|LINK
That sounds simple but the WinForms app is in .NET 3.5 currently and will not be upgraded to 4.5 for quite a while. Any other alternatives? Thanks.
SiggiGG
Member
265 Points
105 Posts
Re: Complex Type Parameter
Mar 07, 2012 02:15 PM|LINK
There are plenty of JSON serializers out there you should be able to use, for example JSON.NET supports .NET 3.5.
http://json.codeplex.com/
SiggiGG
Member
265 Points
105 Posts
Re: Complex Type Parameter
Mar 07, 2012 03:12 PM|LINK
Og btw. I forgot to mention that you can use XML also if thats easier for you :) There is a built in XML formatter in Web API.
jfreemanSTR
Member
6 Points
4 Posts
Re: Complex Type Parameter
Mar 07, 2012 07:34 PM|LINK
Just to clarify things in my head. I have a method in my Web API that accepts 3 parameters (Int, string, ArrayList). I would need to create custom model binder to map data to parameters. Then if I want to call it from a Winforms app, I should serialize the ArrayList (and the other parameters?) in JSON or use XML and then do POST witht the JSON/XML in the message body. Is that correct? Do you have any examples that I could look at to help me get going? Thanks for you help.
SiggiGG
Member
265 Points
105 Posts
Re: Complex Type Parameter
Mar 07, 2012 08:54 PM|LINK
There's no need to use a custom model binder in most cases, but it's an option if you need to do something special.
Don't use the ArrayList type on the method signature. You can use a strongly typed collection or if you dont want to create a class for the data you can use the handy JsonValue type.
Here's an example of how you can parse an array of data using both the methods I described:
public class TestController : ApiController { public int Post(List<DataClass> data) { return data.Count; } public int Put(JsonValue value) { return value.Count; //return data.Count; } } public class DataClass { public string Name { get; set; } public int Age { get; set; } }Example of data to POST/PUT:
[{"Age":20,"Name":"Name1"},{"Age":42,"Name":"Name2"}]
As for the Winforms app, you would have to use something like the WebRequest class since you can't use the new HttpClient. I'm sure there are plenty of examples on how to do that on the web :)
jeff.mounce
Member
7 Points
4 Posts
Re: Complex Type Parameter
Mar 08, 2012 12:37 AM|LINK
Is there an equivalent to JsonValue for XML input?
(follow-up)
If the answer is yes, and the method is declared as such:
public class TestController : ApiController { ... public int Put(XmlDocument value) { return 1; } }..then how do I make Put() agnostic to the input type?
I wouldn't want 2 methods, would I?
public class TestController : ApiController { ... public int Put(JsonValue value) { return 1; } public int Put(XmlDocument value) { return 1; } }I know, I know, this is why we use types/classes as the medium, but if I don't want to declare a complex type....what do I do? punt?
Seems silly to make the input parms dependent on the Content-Type at all (so JsonValue is useless to me).