C
C#13mo ago
Very Funny

Unable to find endpoints. Invalid URI

I have this code on my controller
c#
[Route("api/[controller]")]
[ApiController]
[Authorize(Roles = "Admin")]
public class AdminController : ControllerBase
c#
[Route("api/[controller]")]
[ApiController]
[Authorize(Roles = "Admin")]
public class AdminController : ControllerBase
In other projects https://localhost:<PORT>/api/Admin/... would return the data. But now the URI is invalid. As it is a .NET 8 project, I am unsure as to how to add swagger as well
2 Replies
KuumaKoira
KuumaKoira13mo ago
Do your actions (methods in controller that are supposed to be endpoints) have their own routing too? If not, change line
[Route("api/[controller]")]
[Route("api/[controller]")]
To
[Route("api/[controller]/[action]")]
[Route("api/[controller]/[action]")]
Then the routes will default to action naming e.g: Create() will be accessible under api/admin/create unless you overwrite it
Very Funny
Very FunnyOP13mo ago
These are my endpoints
c#
[HttpGet("GetAllUsers")]
public async Task<ActionResult<List<UserInfo>>> GetAllUsers()
{
return Ok(await _adminService.GetAllUsers());
}

[HttpGet("GetUser/{id}")]
public async Task<ActionResult<UserInfo>> GetUser(string id)
{
return Ok(await _adminService.GetUser(id));
}
c#
[HttpGet("GetAllUsers")]
public async Task<ActionResult<List<UserInfo>>> GetAllUsers()
{
return Ok(await _adminService.GetAllUsers());
}

[HttpGet("GetUser/{id}")]
public async Task<ActionResult<UserInfo>> GetUser(string id)
{
return Ok(await _adminService.GetUser(id));
}
This worked in .NET 7 Fixed, needed .AddControllers and .MapControllers in my Program.cs
Want results from more Discord servers?
Add your server