1

I've generated my Web Api Controller by scaffolding, I added WebApiConfig and Web Api works but returns bad data.

For example: I have some entities - Player with attributes ID name, surname etc. and PositionId, Position with attributes name and ID. If I want to access one position via normal PositionController it works very well but when I'm trying to get access to one position with web api It returns me all object that are connected with this one position.

E.g. I want to get one position with url api/PositionsAPI/1 It should return

{"PositionId":1,"name":"Defensive midfield"}

but it returns

{"Players":[{"Team":{"Players":[{"PlayerId":4,"Name":"Piotrek","Surname":"Kowalski","Nation":"Poland","PositionId":1,"TeamId":1}],"TeamId":1,"Name":"FC. Barcelona","City":"Barcelona","Founded":"1899-11-29T00:00:00"},"PlayerId":2,"Name":"Jan","Surname":"Kowalski","Nation":"Poland","PositionId":1,"TeamId":1},{"Team":{"Players":[{"PlayerId":2,"Name":"Jan","Surname":"Kowalski","Nation":"Poland","PositionId":1,"TeamId":1}],"TeamId":1,"Name":"FC. Barcelona","City":"Barcelona","Founded":"1899-11-29T00:00:00"},"PlayerId":4,"Name":"Piotrek","Surname":"Kowalski","Nation":"Poland","PositionId":1,"TeamId":1}],"PositionId":1,"name":"Defensive midfield"}

So you can see that it returns all players connected with this position. I don't have any idea why it is happening.

This is method from my web api controller that is responsible for api/PositionsAPI/1

[ResponseType(typeof(Position))]
public IHttpActionResult GetPosition(int id)
{
    Position position = db.Positions.Find(id);
    if (position == null)
    {
        return NotFound();
    }

    return Ok(position);
}

2 Answers 2

1

You can return anonymous object with just properties that you need. For example,

var position = db.Positions // Make sure type is var
   .Where(x => x.PositionId == id)
   .Select(x => new 
   { 
      PositionId = x.PositionId,
      name = x.name
   })
   .FirstOrDefault();

Or disable serializing references.

db.Configuration.ProxyCreationEnabled = false;
0
0

It's a little bit difficult without seeing how you modeled your entities, but check if your routing is properly configured. For example, you mentioned:

api/PositionsAPI/1

I'm assuming PositionsAPI is your Controller and your Position object looks like this:

public class Position
{
    public int PositionId {get; set;}
    public string name {get; set;}
}

So you should be seeing the JSON you expect...

Make sure you're hitting the correct method in the correct controller. Try adding a breakpoint on the first line inside your method, run it from Visual Studio and try getting the data via some REST client (like Postman). If you're not hitting the breakpoint by calling that same endpoint then you most likely have a routing problem.

In order to fix that specify Routes for each controller like this:

[RoutePrefix("PositionsAPI")]
public class PositionsAPIController : ApiController

And do this on your methods to ensure you're not hitting some other method:

//GET: api/PositionsAPI/1 (this is just to make it easier for future reference)
[ResponseType(typeof(Position))]
[HttpGet]
[Route("{id}")]
public IHttpActionResult GetPosition(int id)
    {
        Position position = db.Positions.Find(id);
        if (position == null)
        {
            return NotFound();
        }

        return Ok(position);
    }

Hope that helps!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.