[英]Default routing for web api project
When I create a new ASP.NET Core Web Application
and choose API
project template and run it, it routes to http://localhost:64221/weatherforecast
. When I create a new
ASP.NET Core Web Application
and choose API
project template and run it, it routes to http://localhost:64221/weatherforecast
. May I know where it configures the default routing to weatherforecast
web api?我可以知道它在哪里配置默认路由到天气预报
weatherforecast
api? In the configure
method, I don't see any default routing to weatherforecast
.在
configure
方法中,我没有看到到weatherforecast
的任何默认路由。
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
May I know where it configures the default routing to weatherforecast web api?
我可以知道它在哪里配置默认路由到天气预报 web api?
In the Configure
method of Startup class, you can find that endpoints.MapControllers()
method is called, which only maps controllers that are decorated with routing attributes .在 Startup class 的
Configure
方法中,可以发现调用了endpoints.MapControllers()
方法,它只映射了带有路由属性的控制器。
And in WeatherForecastController
class, you would find [Route("[controller]")]
is applied to it, like below.在
WeatherForecastController
class 中,您会发现[Route("[controller]")]
已应用于它,如下所示。
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
Besides, you can check the source code of MapControllers(IEndpointRouteBuilder)
method and know how it works.此外,您可以查看
MapControllers(IEndpointRouteBuilder)
方法的源代码并了解其工作原理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.