簡體   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