简体   繁体   中英

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 . 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();
        });
    }

the route is configured in launchSettings.json, you can find it in properties在此处输入图像描述

and those are the attributes that you can change to get another route

"applicationUrl": "http://localhost:5002","launchUrl": "swagger",

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM