Amazon Ad

Wednesday 5 March 2014

How To Pass List<> Into Web API WebMethod

Hi,

I was struggling with passing List<> type parameter in Web API webmethod. Here is how i was able to resovle it.

1. Here is my model class

public class Users
    {
        [Key]
        public int UserId { get; set; }
        public string Email { get; set; }
        public string Password { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }

2. public List<Users> TestUsers([FromBody]List<Users> users)
        {
            try
            {
                return users;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

3. Here is my json which was passed using curl command, Here is the complete command

curl -H "Content-Type: application/json" -H "Accept: application/json" -X POST -d "[{\"Email\":\"test@test.com\",\"FirstName\":\"Ritesh\",\"LastName\":\"Tandon\",\"UserType\":\"Normal\"},{\"Email\":\"test12@test.com\",\"FirstName\":\"Vedansh\",\"LastName\":\"Tandon\",\"UserType\":\"FB\"}]" "http://localhost:3760/api/Users/TestUsers"

This is how i was able to pass List<> type object in Web API. You can also use HttpClient class for passing json array.

Thanks
Ritesh

No comments:

Post a Comment

Comments are welcome, Please join me on my Linked In account

http://in.linkedin.com/pub/ritesh-tandon/21/644/33b

How to implement Captcha v3 in ASP.NET

 I was facing an issue of dom parsing in my website. I finally resolved it by using Google Captcha V3. Step 1: Get your keys from https:...