Swagger breaks when adding an API Controller to my

2019-08-20 06:11发布

I downloaded a new .Net Core MVC project template from https://aspnetboilerplate.com/Templates, setup everything (database and restored nuget packages) and ran the Host application. All good as I get the Swagger UI going and can see all the standard services.

I then proceeded to create a simple API controller in the Host application:

[Route("api/[controller]")]
[ApiController]
public class FooBarController : MyAppControllerBase
{
    public string HelloWorld()
    {
        return"Hello, World!";
    }
}

And then Swagger fails to load the API definition:

Fetch error
Internal Server Error http://localhost:21021/swagger/v1/swagger.json

If I remove the Route and ApiController attributes, Swagger works again, but my new controller is not displayed. I can access it by going to http://localhost:21021/foobar/helloworld which is probably fine, but I'd like it to show up in Swagger UI.

Am I missing something?

标签: swagger abp
1条回答
迷人小祖宗
2楼-- · 2019-08-20 06:27

This is how you should configure your Swagger in your "Configure(IApplicationBuilder app, IHostingEnvironment env)" method.

#region Swagger COnfiguration
app.UseSwagger();
// Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.), 
// specifying the Swagger JSON endpoint.
app.UseSwaggerUI(c =>
{
    c.SwaggerEndpoint("swagger/v1/swagger.json", "Your class name");
    c.RoutePrefix = string.Empty;
});
#endregion

And here will be your configureServices settings for swagger.

services.AddSwaggerGen(config =>
{
    config.SwaggerDoc("v1", new Info
    {
        Title = "Title Here",
        Version = "v1"
    });
});
查看更多
登录 后发表回答