Nik*_*rov 5 c# swagger asp.net-core asp.net-core-2.1
I have the asp.net core MVC project and separate WebApi project in one solution. I'm adding the swagger following the documentation on github. Here is my Startup.cs of mvc project:
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?}");
});
}
Run Code Online (Sandbox Code Playgroud)
Here are the nugets:
The WebApi controllers Attribute routing:
[Route("api/[controller]")]
public class CategoriesController : Controller
{
// ...
[HttpGet]
public async Task<IActionResult> Get()
{
return Ok(await _repo.GetCategoriesEagerAsync());
}
// ...
}
Run Code Online (Sandbox Code Playgroud)
When I'm trying to go to /swagger it doesn't find the /swagger/v1/swagger.json:

What I'm doing wrong?
Thanks in advance!
小智 3
我在这个问题上被困了几个小时......我找到了原因......
检查下面的代码!
..启动.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();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16382 次 |
| 最近记录: |