I am trying to call a method in a webapiController(.net core) from my test
If my request object has an Id as string it does not work ,as an int it does
what Am I doing wrong in my noddy sample?
[Fact]
public async Task WhyDoesNotWorkWithIdAsString()
{
string thisQueryDoesNotWork = "http://localhost:1111/api/v1/shop/customers?id=1";
string thisQueryWorksProvidedTheIdIsAnInt = "http://localhost:1111/api/v1/shop/customers/1";
var response = await client.GetAsync(thisQueryDoesNotWork);
var response2 = await client.GetAsync(thisQueryWorksProvidedTheIdIsAnInt);
//omitted asserts
}
[Route("api/[controller]")]
public class ShopController: Controller
{
[HttpGet]
[Route("{id}",Name ="GetCustomerAsync")]
[ProducesResponseType(typeof(GetCustomerResponse), (int)HttpStatusCode.OK)]
//more ProducesResponseType omitted
public async Task<IActionResult> GetCustomerAsync([FromQuery]GetCustomerRequest request)
{
//code omitted
}
}
public class GetCustomerRequest
{
Required]
public string Id { get; set; }
// public int Id { get; set; } //works with int but not with a string
}
}
Also is below correct
[FromQuery]=use Get only [FromBody]=use Put-Post
is there a link with explanation when to use this parameter binding?
many thanks