I have a WebAPI controller named WeatherForecast with one operation. The operation method looks like follow:
[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();
}
However, the HttpGet.Name = "GetWeatherForecast" should define a route name "GetWeatherForecast" as I understand the purpose of the Name property for this attribute.
But Swagger shows me that the operation itself has no route at all: The displayed URL is https://localhost:port/WeatherForecast (the service operation can be consumed through that URL, I used Postman for testing)
But with the HttpGet attribute with the Name property set, I would expect it to be https://localhost:port/WeaterhForecast/GetWeatherForecast
When I additionally use the Route attribute (Route("GetWeatherForecast")) on the operation method, then the route for the operation is shown as follows: https://localhost:port/WeaterhForecast/GetWeatherForecast
(the service operation is indeed now accessible through that URL).
So, the question is: Why is the Name property of HttpGet attribute not doing what documentation promised? Or what is HttpGetAttribute.Name really for?
The source code was made with .NET 6.0 with VS 2022, project type ASP.NET Core-Web-API. The shown code is from the automatically created controller by project template.