如何在 ASP.NET Web Api 中设置 swagger basePath?

How to set swagger basePath in ASP.NET Web Api?

我为 ASP.NET 网络应用程序启用了 swagger 文档。如何设置basePath?

GlobalConfiguration.Configuration .EnableSwagger(c => { 
    c.SingleApiVersion("v1", "MyApi") .Description("This is my API."); 
    c.IncludeXmlComments($@"{AppDomain.CurrentDomain.BaseDirectory}\bin\MyApi.XML");
}) .EnableSwaggerUi(c => {});

目前生成的API描述如下:

"swagger": "2.0",
"host": "localhost:8080",
"basePath": "/",

我想改成:

"swagger": "2.0",
"host": "localhost:8080",
"basePath": "/myapi",

您需要将 RootUrl 添加到您的配置中,例如:

GlobalConfiguration.Configuration .EnableSwagger(c => { 
    
    c.SingleApiVersion("v1", "MyApi") .Description("This is my API."); 
    
    c.IncludeXmlComments($@"{AppDomain.CurrentDomain.BaseDirectory}\bin\MyApi.XML");
    
    c.RootUrl(req => { return "http://localhost:8080/myapi"; });
    
}) .EnableSwaggerUi(c => {});