为什么 Rider 认为我的控制器方法未使用?
我是 C# 新手,而且是 Rider,这对我来说很奇怪。
我有一个带有多个映射的控制器 - 仅显示第一个映射,但所有映射的问题都是相同的。
该应用程序工作正常,每个单独的端点在由 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;
}
关于如何强制 Rider 识别方法并删除相关警告有什么想法吗? 提前致谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如其他评论已经指出的那样,这是静态代码分析的局限性。
但由于我个人希望有一个没有 R# 警告的解决方案,因此我使用
[UsedImplicitly]
来自JetBrains.Annotations
NuGet 包。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 theJetBrains.Annotations
NuGet package.