My swagger is not working in my ASP.net core project. It works when I comment out either the put or post action method. I have also tried putting the name of eachpoint to fix the problem but it didnt work. Can anyone tell me how to overcome this issue? Thanks
[HttpPut("{ids}", Name = nameof(Edit))]
[Route("api/[controller]/[action]")]
[ApiController]
public class CompanyController : ControllerBase
{
private readonly IMediator _mediator;
public CompanyController(IMediator mediator)
{
_mediator = mediator;
}
[HttpGet()]
public async Task<ActionResult<List<Company>>> List()
{
return await _mediator.Send(new List.Query());
}
[HttpPost]
public async Task<ActionResult<Unit>> Create(Create.Command command)
{
return await _mediator.Send(command);
}
[HttpGet("{id}")]
[ProducesResponseType((int)HttpStatusCode.OK)]
[ProducesResponseType((int)HttpStatusCode.BadRequest)]
public async Task<ActionResult<Company>> Details(int id)
{
return await _mediator.Send(new Details.Query { Id = id });
}
[HttpPut("{ids}", Name = nameof(Edit))]
public async Task<ActionResult<Unit>> Edit(int ids, Edit.Command command)
{
command.Id = ids;
return await _mediator.Send(command);
}
}
}
This is how I configured swagger in my startup class.
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(opt =>
{
opt.AddPolicy("CorsPolicy", policy =>
{
policy.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials()
.WithOrigins("http://localhost:3000");
});
});
// Register the Swagger generator, defining 1 or more Swagger documents
**services.AddSwaggerGen();**
services.AddControllers()
.AddFluentValidation(cfg =>
{
cfg.RegisterValidatorsFromAssemblyContaining<Create>();
});
services.AddMediatR(typeof(List.Handler).Assembly);
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseCors("CorsPolicy");
// Enable middleware to serve generated Swagger as a JSON endpoint.
**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", "My API V1");
});**
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}