Search code examples
c#controllerrider

Why does Rider think my controller methods are unused?


I'm new to C# and by extension, Rider and this is quite strange to me.

I have a controller with several mappings - only showing the first one, but the problem is the same for all of them.

The application works fine, each of the individual endpoints does its job as expected when triggered by Postman, but for some reason the method names are greyed out and Rider keeps suggesting removing them because they are "unused".

{
[ApiController]
[Route("")]
public class HomeController
{
    private readonly IToDoService _toDoService;

    public HomeController(IToDoService toDoService)
    {
        _toDoService = toDoService;
    }

    [HttpGet("")]
    [HttpGet("/list")]
    public ActionResult<List<ToDo>> ShowTodos()
    {
        var todos = _toDoService.GetAllToDos();
        return todos;
    }

Any ideas on how to force Rider to recognize the methods and remove related warnings? Thanks in advance.


Solution

  • As the other comments already pointed out, this is a limitation of static code analysis.

    But since I personally want to have a Solution free of R# warnings, I explicitly decorate classes like this (e. g. controllers) with the [UsedImplicitly] attribute from the JetBrains.Annotations NuGet package.