I'm developing a .NET Core 3.1 Web API application, and I'm staring at this screen for a while.
I've installed Swashbuckle.AspNetCore -Version 5.5.0 in my project, and I've made the following configurations:
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc(options =>
{
options.Filters.Add(typeof(DomainExceptionFilter));
options.ModelBinderProviders.Insert(0, new ProviderModelBinder());
});
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo
{
Version = "v1",
Title = "Sample .NET Core DDD",
Description = "A .NET Core sample project based on DDD principles",
License = new OpenApiLicense
{
Name = "MIT License",
Url = new Uri("https://opensource.org/licenses/MIT")
}
});
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
c.IncludeXmlComments(xmlPath);
});
services.AddControllers();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "Sample API v1");
c.RoutePrefix = string.Empty;
});
app.UseRouting();
app.UseEndpoints(endpoints => { endpoints.MapControllers(); });
}
I have more things in those methods, but I've added here only what I've found necessary for this issue.
And my controller is this:
UserController.cs
namespace Sample.Application.Controllers
{
[Route("Api/[controller]")]
[ApiController]
public class UserController : ControllerBase<User, UserEditViewModel, UserListViewModel>
{
private readonly IUserService _userService;
public UserController(IUserService service, IMapper mapper) : base(service, mapper)
{
_userService = service;
}
/// <summary>
/// Login the user in the system
/// </summary>
/// <param name="loginViewModel">Login information</param>
/// <returns>Token</returns>
[HttpPost("Login")]
[AllowAnonymous]
[ProducesResponseType(typeof(UserTokenViewModel), 200)]
public IActionResult Authenticate([FromBody]UserLoginViewModel loginViewModel)
{
// Do something
}
}
}
I've tried to use this syntax as well:
[HttpGet]
[Route("Login")]
[AllowAnonymous]
[ProducesResponseType(typeof(UserTokenViewModel), 200)]
public IActionResult Authenticate([FromBody]UserLoginViewModel loginViewModel)
{
// Do something
}
And I've other methods that are inherit from the base controller.
What have I done wrong? Does anyone faced the same issue? I've looked the files from the build, and the XML is being generated with all the information, but swagger seems to ignore it.