我有一个名为的 WebAPI 控制器天气预报只需一次操作。操作方法如下:
[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 = "GetWeatherForecast"
应该定义一个路线名称“GetWeatherForecast”,因为我理解的目的Name
该属性的属性。
但 Swagger 向我展示了操作本身根本没有路径:
显示的网址是https://localhost:端口/WeatherForecast(通过该URL即可消费服务操作,我使用Postman进行测试)
但随着HttpGet
属性与Name
属性集,我希望它是 https://localhost:port/WeatherhForecast/获取天气预报
当我另外使用Route
属性 (Route("GetWeatherForecast")
)在操作方法上,那么操作的路由如下所示:https://localhost:port/WeatherhForecast/获取天气预报(现在确实可以通过该 URL 访问服务操作)。
所以,问题是:为什么Name
的财产HttpGet
属性没有做什么文档承诺的?或者什么是HttpGetAttribute.Name
真的是为了?
源代码是使用 .NET 6.0 和 VS 2022 编写的,项目类型为 ASP.NET Core-Web-API。显示的代码来自通过项目模板自动创建的控制器。