How to fix a simple Asp.net core controller route that is not working

Once in a while I add a new action to my controller and for some reason it does not work, even though I have a whole project filled with controllers and their actions that work. Most often the reason is a slash at the beginning of my actions route. With this the controller will return a 404 for that endpoint.

Breaking it down

An example would be the following controller with the simple action TryItOut:

[Route("api/Test")]
[ApiController]
public class TestController : ControllerBase
{
    [HttpGet("TryItOut")]
    public string Get()
    {
        return "Works!";
    }
}

Using this, I am able to get the URL http://localhost:<port>/api/test/tryitout to return the line Works!. However if you have leading slash in your controllers action this will not work, as demonstrated here:

[Route("api/Test")]
[ApiController]
public class TestController : ControllerBase
{
    [HttpGet("/TryItOut")] //extra preceding slash
    public string Get()
    {
        return "Does not work";
    }
}

The extra slash makes you get a 404 when you try to reach http://localhost:<port>/api/test/tryitout - the same URL as before.

I am not going to elaborate on how many times I have made this mistake, but I hope you can save some time reading this post if you are stuck with a 404 on a single endpoint in your solution.

Let me know if it helped you in the comments!