Home > Software design >  Where is the routing in the example of ASP.Net Core 6 WebApi?
Where is the routing in the example of ASP.Net Core 6 WebApi?

Time:03-31

I've just started looking at webApi core 6, and the weatherforecast example. I'm using Postman and have commented out Swagger and SwaggerUI in Program.cs, the URL https://localhost:7192/WeatherForecast works, so I'm assuming 'WeatherForecast' is the controller name and it defaults to the httpGet method of Get()? But I'm trying to find the routing, because that HttpGet has an attribute name of "GetWeatherForecast" which doesn't work in postman.

Now, I've added a second Get method just to see how this will be hit, like below, but it says error 'AmbiguousMatchException: The request matched multiple endpoints. Matches: ... ' when I used the URL above, but using 'GetBlah' nor 'GetTest' doesn't work as well, so how is the routing mapped?

[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
    private static readonly string[] Summaries = new[]
    {
    "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
    };

    private readonly ILogger<WeatherForecastController> _logger;

    public WeatherForecastController(ILogger<WeatherForecastController> logger)
    {
        _logger = logger;
    }

    [HttpGet(Name = "GetWeatherForecast")]
    public IEnumerable<WeatherForecast> Get()
    {
        return Enumerable.Range(1, 5).Select(index => new WeatherForecast
        {
            Date = DateTime.Now.AddDays(index),
            TemperatureC = Random.Shared.Next(-20, 55),
            Summary = Summaries[Random.Shared.Next(Summaries.Length)]
        })
        .ToArray();
    }

    [HttpGet(Name = "GetTest")]
    public IEnumerable<WeatherForecast> GetBlah()
    {
        return Enumerable.Range(1, 5).Select(index => new WeatherForecast
        {
            Date = DateTime.Now.AddDays(index),
            TemperatureC = Random.Shared.Next(-20, 55),
            Summary = Summaries[Random.Shared.Next(Summaries.Length)]
        })
        .ToArray();
    }

CodePudding user response:

The [Route] attribute in the controller helps you to access /WeatherForecast. And based on the HTTP Request method controller action methods are invoked. Since you're trying to access the /Weatherforecast endpoint - it is will invoke the GET method. When you're putting an attribute like [HttpGet(Name = "GetTest")] you're specifying the name of the action method - it has nothing to do with routing. If you like to expose this method, you need to add it like this. [HttpGet("GetTest",Name = "GetTest")] - the first parameter is the template and second one is the name - it is for Open API.

CodePudding user response:

Below is a work demo, you can refer to it.In program, I donot change app.MapControllers(); I just remove [ApiController], and add route.

   [Route("api/WeatherForecast")]
    public class WeatherForecastController : ControllerBase
    {
        private static readonly string[] Summaries = new[]
        {
        "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
    };

        private readonly ILogger<WeatherForecastController> _logger;

        public WeatherForecastController(ILogger<WeatherForecastController> logger)
        {
            _logger = logger;
        }

        [Route("~/api/Get")]
        [HttpGet(Name = "GetWeatherForecast")]

        public IEnumerable<WeatherForecast> Get()
        {
            return Enumerable.Range(1, 5).Select(index => new WeatherForecast
            {
                Date = DateTime.Now.AddDays(index),
                TemperatureC = Random.Shared.Next(-20, 55),
                Summary = Summaries[Random.Shared.Next(Summaries.Length)]
            })
            .ToArray();
        }
        [Route("~/api/GetBlah")]
        [HttpGet(Name = "GetTest")]

        public IEnumerable<WeatherForecast> GetBlah()
        {
            return Enumerable.Range(1, 5).Select(index => new WeatherForecast
            {
                Date = DateTime.Now.AddDays(index),
                TemperatureC = Random.Shared.Next(-20, 55),
                Summary = Summaries[Random.Shared.Next(Summaries.Length)]
            })
            .ToArray();
        }

    }

result:

enter image description here

  • Related