I’m trying to learn to create an API using ASP.Net Core MVC. So using VS2022 I created the default Weatherforecast API with Swagger enabled.
The controller’s header has
[Route("api/[controller]/[action]")]
[ApiController]
public class WeatherController : ControllerBase
Just for the sake of learning, I added this method:
[HttpGet("{someId}/{someName}/{someDateTime}/someBool")]
public IActionResult Get(int someId, string someName, DateTime someDateTime, bool someBool)
{
return StatusCode(200, $"Value passed: {someId}/{someName}");
}
When I run it the page opens, I see the method in the swagger page, and I click the ‘Try It Out’ button. I fill in all four parameters, click Execute, the API fires and my breakpoint in the controller method is caught. All data is passed in.
In the Swagger page, the Request URL shows
https://localhost:7062/api/Users/Get/65974/Jack%20Smith/1954-03-10/someBool?someBool=false
The method returns “Value passed: 65974/Jack Smith”, as it should.
Evertything looks like it works, bit I’m not convinced this is right.
First, when I go into Postman and fill in paramters on the Params tab and run a Get, it converts the URL to
https://localhost:7062/api/Users/Get?someId=65974&someName=Jack Smith&someDateTime=1954-03-10&someBool=false
which is different than what Swagger shows. And, this fires the parameterless Get method.
What I’m ultimatly trying to acheive is to create an API that can have multiple Get and Post methods that accept different parameters.
Just bcause it fires the controller method doesn’t man it’s right.Do I have this all set up right?
Thanks