【问题标题】:Default routing for web api projectweb api 项目的默认路由
【发布时间】:2020-10-29 14:55:06
【问题描述】:

当我创建一个新的ASP.NET Core Web Application 并选择API 项目模板并运行它时,它会路由到http://localhost:64221/weatherforecast。我可以知道它在哪里配置默认路由到weatherforecast web api?在configure 方法中,我没有看到到weatherforecast 的任何默认路由。

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

        app.UseRouting();

        app.UseAuthorization();

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

【问题讨论】:

    标签: c# asp.net-core asp.net-core-webapi asp.net-core-routing


    【解决方案1】:

    路由在launchSettings.json中配置,可以在properties中找到

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

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

    【讨论】:

      【解决方案2】:

      我可以知道它在哪里配置到 weatherforecast web api 的默认路由吗?

      在Startup类的Configure方法中,可以发现endpoints.MapControllers() 方法被调用,它只映射带有routing attributes 修饰的控制器。

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

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

      此外,您可以查看the source code of MapControllers(IEndpointRouteBuilder) method 并了解其工作原理。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-01-30
        • 1970-01-01
        • 1970-01-01
        • 2020-03-23
        • 2013-04-17
        • 2012-06-25
        相关资源
        最近更新 更多