我有一個 asp.net 核心 web api 專案,我使用的是 3.1 版。我安裝了swagger包并進行了配置,一切正常,但是swagger頁面的API沒有看到,這是為什么,這是我參考檔案的配置資訊。
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<TodoContext>(opt =>
opt.UseInMemoryDatabase("TodoList"));
services.AddControllers();
// Register the Swagger generator, defining 1 or more Swagger documents
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo
{
Version = "v1",
Title = "ToDo API",
Description = "A simple example ASP.NET Core Web API",
TermsOfService = new Uri("https://example.com/terms"),
Contact = new OpenApiContact
{
Name = "Shayne Boyer",
Email = string.Empty,
Url = new Uri("https://twitter.com/spboyer"),
},
License = new OpenApiLicense
{
Name = "Use under LICX",
Url = new Uri("https://example.com/license"),
}
});
// Set the comments path for the Swagger JSON and UI.
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
c.IncludeXmlComments(xmlPath);
});
}
public void Configure(IApplicationBuilder app)
{
// Enable middleware to serve generated Swagger as a JSON endpoint.
app.UseSwagger(c =>
{
c.SerializeAsV2 = true;
});
// Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.),
// specifying the Swagger JSON endpoint.
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
});
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
uj5u.com熱心網友回復:
我根據你的代碼重現了這個問題。我自己創建了一個Controller,但是由于沒有添加路由地址,所以第一次打開是這樣的:

只有一個默認路由,但在我的專案中,我添加了一個 HomeController,它也沒有出現在該 API 中。
public class HomeController : Controller
{
[HttpGet]
public string Index()
{
return "test";
}
}
這是因為我沒有添加路由地址,它沒有顯示。
[Route("api/[controller]")]
public class HomeController : Controller
{
[HttpGet]
[Route("test")]
public string Index()
{
return "test";
}
}
添加后:

不知道你是不是這個原因,如果不是,能詳細解釋一下你的步驟嗎?或者提供一個界面,你的招搖不顯示 API。
參考檔案:
開始使用 Swashbuckle 和 ASP.NET Core
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/409258.html
標籤:
