ASP.Net Core 6 WebApi 例子中的路由在哪里?
Where is the routing in the example of ASP.Net Core 6 WebApi?
我刚刚开始研究 webApi 核心 6 和天气预报示例。我正在使用 Postman 并在 Program.cs 中注释掉了 Swagger 和 SwaggerUI,URL https://localhost:7192/WeatherForecast 有效,所以我假设 'WeatherForecast' 是控制器名称它默认为 Get() 的 httpGet 方法?但我正在尝试查找路由,因为 HttpGet 的属性名称为“GetWeatherForecast”,这在邮递员中不起作用。
现在,我添加了第二个 Get 方法,只是为了看看它是如何被命中的,如下所示,但是当我使用上面的 URL 时它说错误 'AmbiguousMatchException: The request matched multiple endpoints. Matches: ... ',但是使用 'GetBlah' 和 'GetTest' 都不行,那么路由是如何映射的呢?
[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();
}
控制器中的[Route]属性可以帮助您访问/WeatherForecast
。并基于 HTTP 请求方法调用控制器操作方法。由于您正在尝试访问 /Weatherforecast
端点 - 它将调用 GET 方法。当您放置 [HttpGet(Name = "GetTest")]
之类的属性时,您指定的是操作方法的名称 - 它与路由无关。如果你想暴露这个方法,你需要像这样添加它。 [HttpGet("GetTest",Name = "GetTest")]
- 第一个参数是模板,第二个参数是名称 - 它用于打开 API。
下面是工作demo,可以参考it.In程序,我不改app.MapControllers();我只是删除 [ApiController],并添加路由。
[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();
}
}
结果:
我刚刚开始研究 webApi 核心 6 和天气预报示例。我正在使用 Postman 并在 Program.cs 中注释掉了 Swagger 和 SwaggerUI,URL https://localhost:7192/WeatherForecast 有效,所以我假设 'WeatherForecast' 是控制器名称它默认为 Get() 的 httpGet 方法?但我正在尝试查找路由,因为 HttpGet 的属性名称为“GetWeatherForecast”,这在邮递员中不起作用。
现在,我添加了第二个 Get 方法,只是为了看看它是如何被命中的,如下所示,但是当我使用上面的 URL 时它说错误 'AmbiguousMatchException: The request matched multiple endpoints. Matches: ... ',但是使用 'GetBlah' 和 'GetTest' 都不行,那么路由是如何映射的呢?
[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();
}
控制器中的[Route]属性可以帮助您访问/WeatherForecast
。并基于 HTTP 请求方法调用控制器操作方法。由于您正在尝试访问 /Weatherforecast
端点 - 它将调用 GET 方法。当您放置 [HttpGet(Name = "GetTest")]
之类的属性时,您指定的是操作方法的名称 - 它与路由无关。如果你想暴露这个方法,你需要像这样添加它。 [HttpGet("GetTest",Name = "GetTest")]
- 第一个参数是模板,第二个参数是名称 - 它用于打开 API。
下面是工作demo,可以参考it.In程序,我不改app.MapControllers();我只是删除 [ApiController],并添加路由。
[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();
}
}
结果: