In my net core api application all the controllers inherit from a base controller.
Because of this, swagger doesn't work.
I create a sample project with visual studio and swagger works fine
But when I create a base class (Test.cs) that inherits from ControllerBase and I change ValuesController.cs to inherit from Test instead of ControllerBase, swagger don't work.
I got the error
Failed to load API definition. Fetch errorundefined /swagger/v1/swagger.json
Thank you for your help!
Here is my code:
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Info { Title = "Test", Version = "v1" });
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.XML";
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
c.IncludeXmlComments(xmlPath);
});
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseMvc();
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("swagger.json", "Test");
});
}
Test.cs
using Microsoft.AspNetCore.Mvc;
namespace WebApplication6.Controllers
{
public class Test : ControllerBase
{
public void MyTest()
{
}
}
}
ValuesController.cs
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
namespace WebApplication6.Controllers
{
[Route("api/[controller]")]
[ApiController]
// This works
// public class ValuesController : ControllerBase
// This doesn't works
public class ValuesController : Test
{
/// <summary>
/// Test
/// </summary>
/// <returns></returns>
[HttpGet]
public ActionResult<IEnumerable<string>> Get()
{
return new string[] { "value1", "value2" };
}
}
}
I think it will be better to use [NoAction] to tell the Swagger Generator this is not an action, no required to generate. Example, below.
[NonAction]
public OkObjectResult Ok(string message){}