ApiControllerAttribute class failing with "Action methods on controllers annotated with ApiControllerAttribute must be attribute routed."

huangapple go评论70阅读模式
英文:

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&lt;IActionResult&gt; 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.

huangapple
  • 本文由 发表于 2023年4月7日 03:25:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75953082.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定