0

I have class for capturing all parameters :

public class UserParams
{
    private const int MaxPageSize = 50;
    public int PageNumber { get; set; } = 1;
    private int pageSize = 10;
    public int PageSize
    {
        get { return pageSize;}
        set { pageSize = (value > MaxPageSize) ? MaxPageSize : value;}
    }

    public int UserId { get; set; }        
    public string OrderBy { get; set; }
    public bool isAll {get; set;} = true; 
    public bool isManagers { get; set; } = false;
    public bool isPartners { get; set; } = false;
}

and my controllers:

[HttpGet]
public async Task<IActionResult> GetUsers([FromQuery] UserParams userParams)
{
    var users = await _appRepo.GetUsers(userParams);
    var usersDto = _mapper.Map<IEnumerable<UserSimpleDto>>(users);
    Response.AddPagination(users.CurrentPage, users.PageSize, users.TotalCount, users.TotalPages);
    return Ok(usersDto);
}    

While my appRepo.GetUsers:

public async Task<PagedList<User>> GetUsers(UserParams userParams)
{
    var users = _context.Users.OrderByDescending(p => p.FirstName).AsQueryable();
    if(userParams.isManagers)
    {
        users = users.Where( u => u.IsManager == true);
    }

    if(userParams.isPartners)
    {
        users = users.Where( u => u.IsPartner == true);
    }

    return await PagedList<User>.CreateAsync(users, userParams.PageNumber, userParams.PageSize);
}

I tried to debug the code with Postman : http://localhost:5000/api/user?isManagers=1 or http://localhost:5000/api/user?isManagers=true

but it seems the params isManagers still false (the result is still get all users)

How to fix this bug?

5
  • Possible duplicate of ASP.NET Core: [FromQuery] usage and URL format Commented Jul 18, 2018 at 17:49
  • See this stackoverflow.com/questions/49741284/…. Basically, FromQuery only works with built-in primative types out of the box. If you want to do what you have there, you need to make a custom model binder. Commented Jul 18, 2018 at 17:50
  • i saw example from udemy course where angular send data using httpclient and using httpparams for user params. they catch the userparams in dotnet core using this way. but i have no idea how to test it using postman. Commented Jul 19, 2018 at 5:20
  • @nightingale2k1 your code works fine for me. Also, your method is called GetUsers, why do you use just 'user' in Postman calls? Commented Jul 19, 2018 at 6:08
  • @AlexRiabov because my controller: [Route("api/[controller]")] public class UserController : Controller so everytime I call /api/user it will call method with HtttpGet altough I put name getUsers Commented Jul 19, 2018 at 6:45

0

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.