简体   繁体   中英

ASP.NET Core WebAPI 3.1 multiple parameters vs Complex object httpget

Not done API for a while and wanted to make sure what is the best practice to make a call when you need to pass multiple parameters in an HttpGet

Option 1

[HttpGet("getpet", Name = nameof(GetPet))]
[ProducesResponseType(typeof(PetResponse), (int)HttpStatusCode.OK)]
public async Task<ActionResult<<PetResponse>> GetById(
[FromQuery]int id,
[FromQuery]bool dogsOnly)

Option 2 use a Complex Object.

[HttpGet("getpet", Name = nameof(GetPet))]
[ProducesResponseType(typeof(PetResponse), (int)HttpStatusCode.OK)]
public async Task<ActionResult<<PetResponse>>  GetById([FromQuery]PetRequest request)

public class PetRequest { public int Id { get; set; } public bool DogsOnly { get; set; } }

Any suggestions or limitation of any of the approach eg test in postman?

Any suggestions or limitation of any of the approach eg test in postman?

The two option use the same way to test on Postman.

If your query strings would not change,the two options are all acceptable.But if you need to change the query string afterwards and the same query strings appear many times in your application,creating a PetRequest model is much better.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM