gpt4 book ai didi

c# - Swagger 没有生成 swagger.json

转载 作者:行者123 更新时间:2023-11-30 21:32:38 24 4
gpt4 key购买 nike

我在一个解决方案中有 asp.net 核心 MVC 项目和单独的 WebApi 项目。我正在按照 github 上的文档添加 swagger。这是我的 mvc 项目的 Startup.cs:

public void ConfigureServices(IServiceCollection services)
{
//...
// Adding controllers from WebApi:
var controllerAssembly = Assembly.Load(new AssemblyName("WebApi"));
services.AddMvc(o =>
{
o.Filters.Add<GlobalExceptionFilter>();
o.Filters.Add<GlobalLoggingFilter>();
})
.AddApplicationPart(controllerAssembly)
.AddJsonOptions(options =>
{
options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
});

services.AddSwaggerGen(c =>
{
//The generated Swagger JSON file will have these properties.
c.SwaggerDoc("v1", new Info
{
Title = "Swagger XML Api Demo",
Version = "v1",
});
});

//...
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
//...
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "Swagger XML Api Demo v1");
});

//...

app.UseMvc(routes =>
{
// ...
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}

以下是核心内容:

nugets

WebApi Controller 属性路由:

[Route("api/[controller]")]
public class CategoriesController : Controller
{
// ...
[HttpGet]
public async Task<IActionResult> Get()
{
return Ok(await _repo.GetCategoriesEagerAsync());
}
// ...
}

当我尝试访问/swagger 时,它找不到/swagger/v1/swagger.json: Json not found

我做错了什么?

提前致谢!

最佳答案

我在这个问题上被困了几个小时......我找到了原因......

检查下面的代码!!

..Startup.cs..

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

app.UseHttpsRedirection();
app.UseMvc();
app.UseSwagger(); // if I remove this line, do not work !
app.UseSwaggerUi3();
}

关于c# - Swagger 没有生成 swagger.json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52162321/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com