英文:
ApiControllerAttribute class failing with "Action methods on controllers annotated with ApiControllerAttribute must be attribute routed."
问题
我的代码如下:
```C#
public class Controller
{
public Controller( )
{
this.prepareAsync();
}
public virtual async Task prepareAsync(){
}
[HttpGet]
public virtual async Task<IActionResult> AggregateAsync()
{
}
}
我有一个虚拟方法,在单元测试中会模拟返回prepareAsync的输出。但是构建失败,出现错误System.Reflection.TargetInvocationException: 目标调用引发了异常...prepareAsync没有属性路由。带有ApiControllerAttribute注解的控制器上的操作方法必须具有属性路由。
有什么解决方法吗?唯一的方法是将prepareAsync方法移到另一个文件吗?我是否无法添加标签来告诉构建prepareAsync只是一个辅助方法?
<details>
<summary>英文:</summary>
My code is like this:
```C#
public class Controller
{
public Controller( )
{
this.prepareAsync();
}
public virtual async Task prepareAsync(){
}
[HttpGet]
public virtual async Task<IActionResult> AggregateAsync()
{
}
}
i have a virtual method I mock in my unit test to return the output of prepareAsync. But the build is failing with error System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation ...prepareAsync does not have an attribute route. Action methods on controllers annotated with ApiControllerAttribute must be attribute routed.
What are some ways around this? is the only way to move the prepareAsync method to another file? Is there no tag i can add to tell the build that prepareAsync is just a helper method?
答案1
得分: 1
请注意,在文档中的 prepareAsync
方法上添加一个 [NonAction]
属性。
来自文档的描述:
表示控制器方法不是一个操作方法。
英文:
Add a [NonAction]
attribute to that prepareAsync
method.
From the documentation:
> Indicates that a controller method is not an action method.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论