2

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

2
  • Is anyone of my suggestion worked for you? Commented Mar 9, 2015 at 22:59
  • Yes - I can't believe I missed that. Thanks for the help. Commented Mar 10, 2015 at 11:46

2 Answers 2

7

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()
    {
Sign up to request clarification or add additional context in comments.

Comments

4

I got this error because I was missing { get; set; } in the properties.

Changed:

public int ID;

To:

public int ID { get; set; }

1 Comment

We were migrating projs from .NETCore 2_1 to .NET 6, this indeed saved a lot of time, still wondering how it worked without the setters and getters earlier.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.