Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.Net WebAPI Returning Empty Objects In List

I am making a GET request to a WebAPI and returning an object like so: http://s12.postimg.org/leoykz3rh/debug1.png

However, when I am retrieving the object on the client side, the objects in Elements are empty with no structure: http://s12.postimg.org/cie6h1d59/debug2.jpg

Here is the class definition of the objects contained in Elements

public class LineElement
    {
        int ID { get; set; }
        int X { get; set; }
        int Y { get; set; }
        int W { get; set; }
        int H { get; set; }
        string Code { get; set; }
        string Color { get; set; }

        public LineElement(int pID, int? pX, int? pY, int? pW, int? pH, string pCode, string pColor)
        {
            ID = pID;
            X = Convert.ToInt16(pX);
            Y = Convert.ToInt16(pY);
            W = Convert.ToInt16(pW);
            H = Convert.ToInt16(pH);
            Code = pCode;
            Color = pColor;
        }
    }

They are added to Elements like so:

var elemList = new List<LineElement>(); // list of LineElements to return
for (var i = 0; i < r.Count(); i++)
                {
                    elemList.Add(new LineElement(r[i].ID, r[i].P_L, r[i].P_T, r[i].P_W, r[i].P_H, r[i].Code.ToString(), r[i].GetSColor()));
                }

And as you can see from the VS debugger screenshot, the object looks fine on the server-side. Any thoughts on what may be happening during serialization or de-serialization?

Also, I have tested the serialization by passing it back to the client using IHttpActionResult which gives me the same result.

return Ok(response);

Thanks

like image 640
jaredcnance Avatar asked Sep 21 '26 01:09

jaredcnance


1 Answers

Try the following,

a. Make properties public.

  public int ID { get; set; }

b. Return the value as shown below,

 return Request.CreateResponse(HttpStatusCode.OK, lineElementList);

c. Make sure you have HttpGet attribute in the method.

    [HttpGet]
    public HttpResponseMessage Methodname()
    {
like image 149
Vim Avatar answered Sep 23 '26 17:09

Vim



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!