简体   繁体   English

为什么 Rider 认为我的 controller 方法未被使用?

[英]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.我是 C# 的新手,推而广之,Rider,这对我来说很奇怪。

I have a controller with several mappings - only showing the first one, but the problem is the same for all of them.我有一个带有多个映射的 controller - 只显示第一个,但所有映射的问题都是一样的。

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".该应用程序运行良好,当 Postman 触发时,每个单独的端点都按预期工作,但由于某种原因,方法名称显示为灰色,Rider 一直建议删除它们,因为它们“未使用”。

{
[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?关于如何强制 Rider 识别方法并删除相关警告的任何想法? Thanks in advance.提前致谢。

As the other comments already pointed out, this is a limitation of static code analysis.正如其他评论已经指出的那样,这是 static 代码分析的局限性。

But since I personally want to have a Solution free of R# warnings, I explicitly decorate classes like this (eg controllers) with the [UsedImplicitly] attribute from the JetBrains.Annotations NuGet package.但是因为我个人想要一个没有 R# 警告的解决方案,所以我使用JetBrains.Annotations NuGet package 中的[UsedImplicitly]属性显式修饰类(例如控制器)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在JetBrains Rider中查找未使用的公共方法 - Find unused public methods in JetBrains Rider 为什么 Rider 会在我的公共变量中添加“@”? - Why does Rider add an “@” to my public variable? 在我的项目中查找所有未使用的代码 (Rider/VSCode) - Find all unused code in my project (Rider/VSCode) LINQ to SQL为什么在尝试将新对象添加到数据库时认为我的新对象为空? - Why does LINQ to SQL think think that my new object is null when I try to add it to the database? 为什么IntelliSense认为我的字典中的值是动态的? - Why does IntelliSense think a value from my dictionary is a dynamic? 为什么我的重载控制器方法无法正常运行? - Why my overload controller methods not hitting properly? 为什么 Rider 建议我使用解构? - Why does Rider suggests me to use deconstruction? 为什么C#编译器仍然从未使用的方法中读取编译代码? - Why does the c# compiler still read compile code from unused methods? 为什么MVC会忽略继承的控制器中的控制器方法? - Why does MVC ignore controller methods in inherited controllers? 当我传递HashSet时,为什么我的泛型类型方法认为T是system.object - Why does my generic type method think T is system.object when I'm passing in a HashSet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM