0

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.

  • 1
    They just consider the explicit calls as references, so, endpoints were not used explicitly in the application. – Ahmad Javadi Nezhad Mar 03 '22 at 14:57
  • That's one of the classic issues Rider/ReSharper has, and is caused by limitation of static code analysis (which cannot detect runtime references via MVC pattern). You will get used to such and many more. – Lex Li Mar 03 '22 at 15:03
  • Your answers aren't making me happy, but at least I understand now. Thank you both. – YanariYoukai Mar 03 '22 at 15:09

1 Answers1

0

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.

mu88
  • 4,156
  • 1
  • 23
  • 47