To break it down, this is my first time using ASP.NET Web Api. I am trying to just get simple tasks down and then from there I will understand more things and be able to work with it, I just need a starting ground. So to begin a simple task for me would be to get a list of Users in a database. I want the JSON result do something like return an object called Repsonse that holds an array of User objects, with each User object storing just like a first and last name. Please anything that can help me understand how to achieve what I am looking for would be appreciated. List code samples can be viewed at api.holybreadstick.com/api/users
Really would be okay if this would return the information without the k_BackingField? If that can be resolved that will help a lot.
This is my ResponseController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
namespace PublicWebApi.Controllers
{
public class UsersController : ApiController
{
Response response = new Response();
public Response GetAllUsers()
{
return response;
}
}
}
This is my Response.cs class
using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.ComponentModel;
using System.Collections.Generic;
namespace PublicWebApi
{
public class Response
{
public List<User> Data = new List<User>();
public Response()
{
allReturn();
}
public void allReturn()
{
for (int x = 0; x < 5; x++)
{
User objet = new User("Bailey", x);
Data.Add(objet);
}
}
}
[Serializable()]
public class User
{
public String name { get; set; }
public int id { get; set; }
public User(String n, int id)
{
name = n;
this.id = id;
}
}
}
So right now this is my result I am getting in JSON.
{
"Data": [
{
"<name>k__BackingField": "Bailey",
"<id>k__BackingField": 0
},
{
"<name>k__BackingField": "Bailey",
"<id>k__BackingField": 1
},
{
"<name>k__BackingField": "Bailey",
"<id>k__BackingField": 2
},
{
"<name>k__BackingField": "Bailey",
"<id>k__BackingField": 3
},
{
"<name>k__BackingField": "Bailey",
"<id>k__BackingField": 4
}
]
}