Guys i have a simple backend codes and i just wanted to integrate swagger ui but it only recognizes 3 Controller from 6 and 3 schema from 7 but when i try to make get request with postman it gives http 404 error. What did i do wrong? I'm using Win11 and .net 5 with Visual Studio 2019.
startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "SwaggerApi", Version = "v1" });
});
services.AddCors();
services.AddHttpContextAccessor();
services.AddSwaggerGen();
services.AddDependencyResolvers(new ICoreModule[] {
new CoreModule()
});
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseCors(builder => builder.WithOrigins("http://localhost:5000").AllowAnyHeader());
app.UseHttpsRedirection();
app.UseWebSockets();
app.UseRouting();
app.UseAuthentication();
app.UseStaticFiles();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "Sample API");
});
program.cs
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseServiceProviderFactory(new AutofacServiceProviderFactory())
.ConfigureContainer<ContainerBuilder>(builder =>
{
builder.RegisterModule(new AutofacBusinessModule());
})
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
Edit: i didn't place my controllers
this is recognized controller sample usersController.cs
[Route("api/[controller]")]
[ApiController]
public class UsersController : ControllerBase
{
private IUserService _userService;
public UsersController(IUserService userService)
{
_userService = userService;
}
[HttpGet("getall")]
public IActionResult Get()
{
var result = _userService.GetAll();
if (result.Success)
{
return Ok(result.Data);
}
return BadRequest(result.Message);
}
[HttpGet("getbyid")]
public IActionResult Get(int id)
{
var result = _userService.GetById(id);
if (result.Success)
{
return Ok(result);
}
return BadRequest(result.Message);
}
[HttpPost("add")]
public IActionResult PostAdd(User user)
{
var result = _userService.Add(user);
if (result.Success)
{
return Ok(result.Message);
}
return BadRequest(result.Message);
}
[HttpPost("update")]
public IActionResult PostUpdate(User user)
{
var result = _userService.Update(user);
if (result.Success)
{
return Ok(result);
}
return BadRequest(result.Message);
}
[HttpPost("delete")]
public IActionResult PostDelete(User user)
{
var result = _userService.Delete(user);
if (result.Success)
{
return Ok(result);
}
return BadRequest(result.Message);
}
}
this is not recognized sample
systemStavesController.cs
[Route("api/[controller]")]
[ApiController]
public class SystemStavesController : ControllerBase
{
private ISystemStaffService _staffService;
public SystemStavesController(ISystemStaffService staffService)
{
_staffService = staffService;
}
[HttpGet("getall")]
IActionResult GetAll()
{
var result = _staffService.GetAll();
if (result.Success)
{
return Ok(result.Data);
}
return BadRequest(result.Message);
}
[HttpGet("getbyid")]
IActionResult GetById(int id)
{
var result = _staffService.GetById(id);
if (result.Success)
{
return Ok(result.Data);
}
return BadRequest(result.Message);
}
[HttpPost("add")]
IActionResult Add(SystemStaff staff)
{
var result = _staffService.Add(staff);
if (result.Success)
{
return Ok(result.Message);
}
return BadRequest(result.Message);
}
[HttpPost("delete")]
IActionResult Delete(SystemStaff staff)
{
var result = _staffService.Delete(staff);
if (result.Success)
{
return Ok(result.Message);
}
return BadRequest(result.Message);
}
[HttpPost("update")]
IActionResult Update(SystemStaff staff)
{
var result = _staffService.Update(staff);
if (result.Success)
{
return Ok(result.Message);
}
return BadRequest(result.Message);
}
}
CodePudding user response:
One difference I can notice between the controller that works and the one that doesn't is that the SytemStavesController
doesn't have any access modifiers for any of the methods, whereas UsersController
have all the declared methods with public
access. Could be the level of restriction from allowing you to access the controller endpoints.