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? In the configure
method, I don't see any default routing to 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?
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 .
And in WeatherForecastController
class, you would find [Route("[controller]")]
is applied to it, like below.
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
Besides, you can check the source code of MapControllers(IEndpointRouteBuilder)
method and know how it works.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.