2

I am sending request. And i want map parameters to C# class in Asp.Net Core Web.API controller. When i write to my method property name as parameter, it is working. But, i write class then it gave me error like "The input was not valid."

I am sending my request by Postman as "Get"request. My request is http://localhost:5002/api/user/GetUsers?PageFirstIndex=0&IsSortAscending=true&PageSize=10&SortBy=Id

When i wrote like this, it is working and all parameters came with value.

public async Task<ServiceResult> GetUsers(string SortBy, bool IsSortAscending, int Page, byte PageSize)
{...}

But when i wrote like this as class, gave me error "The input was not valid.".

[HttpGet("GetUsers")]
public async Task<ServiceResult> GetUsers(QueryObject queryFilter)
{...}


public class QueryObject
{
   public string SortBy { get; set; }
   public bool IsSortAscending { get; set; }
   public int PageFirstIndex { get; set; }
   public byte PageSize { get; set; }
}
3
  • You should be use FromUri in api Commented Aug 17, 2018 at 13:07
  • GetUsers([FromUri] QueryObject queryFilter) ... This will work Commented Aug 17, 2018 at 13:09
  • Thanks but [FromUri] not worked. But [FromQuery] worked. Commented Aug 17, 2018 at 13:21

1 Answer 1

1

You need to use the FromQuery attribute.

public async Task<ServiceResult> GetUsers([FromQuery]QueryObject queryFilter) {}

See the model binding documentation.

Sign up to request clarification or add additional context in comments.

3 Comments

Fred, Is there any way get rid of all [FromQuery] and [FromBody]? For example all get request [FromQuery] and all post requests [FromBody] by Default. Otherwise, i have to write all get method [FromQuery] @Fred
If you decorate the controller with the [ApiController] attribute (new in 2.1) it will attempt automatic interference. learn.microsoft.com/en-us/aspnet/core/web-api/…
Thanks a lot. But on the contrary, I remove [ApiController] attribute from my controller and then it worked. @Fred

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.