Swagger/Openapi静态文件未出现
我正在尝试使dotnet 6天气API示例产生静态摇晃。json或Swagger.yaml。
我很确定我已经遵循了Microsoft文档的步骤,但是没有生成静态文件,我希望它们会出现在构建中。
Microsoft文档URL:
https://learn.microsoft.com/en-us/aspnet/core/core/tutorials/getting-started-with-with-with-swashbuckle?view=
asspnetcore-6.0&tabs = visual-studio 我的 program.cs
:
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddRazorPages();
builder.Services.AddControllersWithViews();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
app.UseHsts();
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseAuthorization();
app.MapDefaultControllerRoute();
app.MapRazorPages();
app.Run();
我正在通过Visual Studio 2022以及 dotnet Run
来运行/构建应用程序
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如下所示,更改您的
.csproj
文件。并请结果。Change your
.csproj
file like below. And please the result.对于没有任何控制器或API的ASP.NET Core Web应用程序项目,将不会生成Swagger文档。
因此,它的一点取决于您要实现的目标。
如果您想在没有API的构建时间生成Swagger Document 是您的解决方案。
但是,如果您在运行时正在寻找生成Swagger文档,则必须添加一些控制器或API才能生成,并且您将不需要任何项目设置更改。
因此,在您的示例中,我可以生成Swagger.json。当然,
在App.maprazorpages()之后,我不得不将摇晃的东西放在此块之外。添加这些行并运行应用程序。
然后转到您的招摇页面
https:// localhost:7235/swagger/index.html
您可以在Swagger UI中看到您的API。
For a ASP.NET core web app project which does not have any controller or APIs swashbuckle will not generate the swagger document.
So its little bit depends on what you want to achieve.
if you want to generate swagger document at the build time with no APIs then the solution https://stackoverflow.com/a/72654660/9247039 is the solution for you.
but if you are looking for generating swagger document during runtime then you have to add some controller or APIs to get it generated and you will not need any project setting changes.
so with simple change in your example I was able to generated swagger.json. and of course I had to put swagger stuff outside this block
after app.MapRazorPages(); add these lines and run the application.
and then go your swagger page
https://localhost:7235/swagger/index.html
you can see your api in swagger ui.