Swagger breaks when adding an API Controller to my Host Project in aspnetboilerplate
Asked Answered
W

2

1

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?

Wife answered 30/5, 2019 at 8:41 Comment(5)
Have you tried just removing the [ApiController] and just using the route? And what is MyAppControllerBase inheriting from?Caen
Thanks for your reply. Yip, removing [ApiController] has no effect. MyAppControllerBase inherits from AbpController from the Abp framework. Seems I have found some solution, though. I added options.ResolveConflictingActions(apiDescriptions => apiDescriptions.First()); to services.AddSwaggerGen in Startup.cs. This seems to do the trick.Wife
ResolveConflictingActions is used when you have multiple actions and Swagger isnt able to determine which one its supposed to use.. However, your example code doesnt have any endpoints? If "HelloWorld" is meant to be an endpoint - it needs the [HttpGet] attribute applied..Caen
Also, I'm not sure if the Abp Framework has the correct Controller base class (Im not familiar with the library).. But that could potentially be another source of the problem. Swagger uses the ApiExplorer functionality under the hood - andrewlock.net/introduction-to-the-apiexplorer-in-asp-net-core .. So, your controller setup needs to adhere to that setup tooCaen
Perfect, thank. I will definitely look into the apiexplorer, and yes, the helloworld was a simple endpoint to test with, I did have to apply [HttpGet] to get it working. As for ABP, it generates api endpoints dynamically. It is quite configurable, but those are mainly used for the web application, and can be used for any other client, yet my needs require a few other rules to be enforced when a 3rd party client connects. Hence the 'custom' API endpoint.Wife
H
0

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"
    });
});
Hydrolyte answered 6/7, 2019 at 12:38 Comment(0)
C
0

Following setting will help fix the issue (both settings are required):

  1. Set the Route attribute at Controller level
  2. Set the Route attribute at Action level

This will help app.MapControllers() resolve the route when there are actions using the same Http verb.

Sample code screenshot is given below:

Setting the routing to resolve Swagger issue

The calls required on services and app are as given below:

// calls on the IServiceCollection
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

// calls on the app object
app.UseSwagger();
app.UseSwaggerUI();

Note: I have tested this on VS 2022 with .Net 8

Chilt answered 15/12, 2023 at 6:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.