繁体   English   中英

web api 项目的默认路由

[英]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 . 我可以知道它在哪里配置默认路由到天气预报weatherforecast api? configure方法中,我没有看到到weatherforecast的任何默认路由。

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseRouting();

        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
        });
    }

路由在launchSettings.json中配置,可以在属性中找到在此处输入图像描述

这些是您可以更改以获得另一条路线的属性

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

我可以知道它在哪里配置默认路由到天气预报 web api?

在 Startup class 的Configure方法中,可以发现调用了endpoints.MapControllers()方法,它只映射了带有路由属性的控制器。

WeatherForecastController class 中,您会发现[Route("[controller]")]已应用于它,如下所示。

[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{ 

此外,您可以查看MapControllers(IEndpointRouteBuilder)方法的源代码并了解其工作原理。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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