Search code examples
c#asp.net-apicontroller

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


My code is like this:

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?


Solution

  • Add a [NonAction] attribute to that prepareAsync method.

    From the documentation:

    Indicates that a controller method is not an action method.