0

I have created an ASP.NET Core 7 Web API and successfully created about 30 endpoints which are working fine. Just got stuck in some weird situation that I have created one endpoint with this signature:

[HttpPost]
[Route("[action]")]
public async Task<IActionResult> UsersBySearch([FromBody] UserSearchOptions prmUSO)
{
    var (Result, Response) = await UOW.Users.GetUsersBySearch(prmUSO);

    if (Result != null)
    {
       return Ok(Response);
    }
    else
    {
       return BadRequest(new ServiceErrorMsg { Message = new() { "Error fetching user list"}});
     }
}

where the UserSearchOptions class looks like this:

public class UserSearchOptions
{
    public int Skip;
    public int PageSize;
    public string? SortCol;
    public string? SortColDir;
    public string? searchVal;
    public int UserId;
}

Now when I run this, my Swagger API not showing this API with input class object as input parameter, also when I call it with Postman, the API is getting called but the parameter values are not receiving in it.

enter image description here

enter image description here

enter image description here

Could some please help to sort out this problem.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Shubhit304
  • 181
  • 1
  • 15

2 Answers2

3

Try adding the getter and setter to your class.

public class UserSearchOptions
{
    public int Skip { get; set; }
    public int PageSize { get; set; }
    public string? SortCol { get; set; }
    public string? SortColDir { get; set; }
    public string? searchVal { get; set; }
    public int UserId { get; set; }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
2

You can either change your DTO to use properties instead of fields as per the other answer, or if you really want to use fields you can enable them by setting JsonSerializerOptions.IncludeFields to true for your API as described by https://stackoverflow.com/a/58139922/1064169.

Martin Costello
  • 9,672
  • 5
  • 60
  • 72