0

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
    }
  ]
} 
  • Did you change any settings regarding serialization of data? That ouput is really weird. – Camo Apr 12 '16 at 05:57
  • No this was just from new project asp web application empty application then checking api at the bottom. – Bailey Miller Apr 12 '16 at 05:59
  • Remove the `[Serializable()]` and try again. – Camo Apr 12 '16 at 06:03
  • Alright that works now for getting a JSON result, however, the default XML view in chrome does not work. – Bailey Miller Apr 12 '16 at 06:05
  • My true desire is for the JSON result only so this is not a major problem just I don't understand why breaking on thing fixes another. – Bailey Miller Apr 12 '16 at 06:05
  • 1
    Look at the accepted answer here and try it: http://stackoverflow.com/a/12359203/4993632 – Camo Apr 12 '16 at 06:06
  • I think this answers the question I asked now I need to learn how to work with POST and adding things into the database with POST calls to the web server. Thanks all for answering. – Bailey Miller Apr 12 '16 at 07:31

0 Answers0