C# .Net 6 Can't hit API route

I’m not able to hit my api routes on new project I configured even though everything looks right. Controller looks like

{
    [Route("api/[controller]")]
    [ApiController]
    public class AccountController : ControllerBase
    {
        private readonly IMapper _mapper;
        private readonly UserManager<User> _userManager;
        private readonly SignInManager<User> _signInManager;
        private readonly ILogger<AccountController> _logger;

        public AccountController(IMapper mapper, UserManager<User> userManager, SignInManager<User> signInManager, ILogger<AccountController> logger)
        {
            _mapper = mapper;
            _userManager = userManager;
            _signInManager = signInManager;
            _logger = logger;
        }



        [HttpGet("/Test")]
        public String Test()
        {
            var z = 7;
            return "Route Hit";
        }

and I’ve tried various requests such as

https://localhost:5001/api/account/test
https://localhost:5001/api/account/Test
https://localhost:5001/api/Account/test
https://localhost:5001/api/Account/Test
https://localhost:5001/api/AccountController/Test
https://localhost:5001/api/AccountController/test

but all of them return a 404 eerror. Why might that be every thing looks correct from what I see. This is on a project I was converting from MVC app to Web API

Example pic

>Solution :

By request of the OP.

This should be configurated in route file

But the slash is taken from the root, so you must remove it to made it as relative path.

    [HttpGet("Test")]
    public String Test()
    {
        var z = 7;
        return "Route Hit";
    }

or complete the sequence from the route.

    [HttpGet("/api/account/Test")]
    public String Test()
    {
        var z = 7;
        return "Route Hit";
    }

Leave a Reply