I have an issue in my api method. I have a method GetDrones which is not working correctly (there is no adequate logic here. I've just made it as short as I could to make it simplier) [way 0]:

[Produces("application/json")]
[Route("api/[controller]")]
public class OwnersController : Controller
{
    private readonly ApplicationDbContext _context;
    private readonly ClaimsPrincipal _caller;

    public OwnersController(ApplicationDbContext context, IHttpContextAccessor httpContextAccessor)
    {
        _context = context;
        _caller = httpContextAccessor.HttpContext.User;

    }
    [HttpGet("GetOwnersDrones")]
    public List<Drone> GetDrones()
    {
        var userId = _caller.Claims.Single(c => c.Type == "id");
        var customer = _context.Customers.Single(c => c.Identity.Id == userId.Value);
        var owner = _context.Owners.Single(o => o.CustomerId == customer.Id);


        return _context.Drones.ToList();
    }
}

enter image description here enter image description here
But if I change the method this way [way 1]:

[HttpGet("GetOwnersDrones")]
public List<Drone> GetDrones()
{
    var userId = _caller.Claims.Single(c => c.Type == "id");
    var customer = _context.Customers.Single(c => c.Identity.Id == userId.Value);
    var owner = _context.Owners.Single(o => o.CustomerId == customer.Id);


    return null;
}

enter image description here Or that way [way 2]:

[HttpGet("GetOwnersDrones")]
public List<Drone> GetDrones()
{
    var userId = _caller.Claims.Single(c => c.Type == "id");
    var customer = _context.Customers.Single(c => c.Identity.Id == userId.Value);
    //var owner = _context.Owners.Single(o => o.CustomerId == customer.Id);


    return _context.Drones.ToList();
}

enter image description here Both are working (no error pops up). Thus, I conclude that

_context.Drones.ToList();

and

_context.Owners.Single(o => o.CustomerId == customer.Id);

doesn't work when in one method? How can I resolve this and make the primordial method work?

UPDATE

[HttpGet("GetOwnersDrones")]
public Owner GetOwnersDrones()
{
    var userId = _caller.Claims.Single(c => c.Type == "id");
    var customer = _context.Customers.Single(c => c.Identity.Id == userId.Value);
    var owner = _context.Owners.SingleOrDefault(o => o.CustomerId == customer.Id);

    return owner;
}

enter image description here

DEBUG
I've debuged through the method. As you can see the drones list and owner is not empty but still I recieve 'Could not get any response' enter image description here

1 Answers

0
Avi Meltser On

It's likely that this line:

    var owner = _context.Owners.Single(o => o.CustomerId == customer.Id);

did not find any matches for the query and threw an exception. I would recommend reading about LINQ's Single and SingleOrDefault methods to understand their behavior better.

By not debugging your code (check out Attacking To Process to debug Web API services) you miss all the necessary information you need in order to resolve your issues.

You have to pay attention to the exceptions thrown in the controller.