'ASP.NET Core api controller not being instantiated

I've created an API controller in my .NET Core 3.1 application (with an Angular frontend, C#).

For some strange reason its not being instantiated, if I try to call any methods on the controller from my Typescript service, nothing happens, it just skips past the call, no error message is generated but the method in the controller isn't accessed.

I've traced it to the fact that the controller isn't being instantiated but I can't see why. Has anyone else experienced this issue?

I inject a service into the constructor, but the service is being added to ioc at startup so it cant be that (along with the other services used), can anyone help?

This is part of the controller code, I've added a breakpoint to the constructor, but its not being hit. I had exactly the same issue with a previous controller I had added, I spent ages trying to figure out why it wasn't being instantiated, then suddenly, it was, despite the fact that I had made no code changes, so I'm baffled by this.

public class RepController : BaseApiController
{
    private readonly IRepService _repService;
    private readonly ILookupService _lookupService;
    private readonly IUserContext _userContext;

    public RepController(IRepService repService,
        ILookupService lookupService,
        IUserContext userContext)
    {
        Assert.NullCheck(repService);
        Assert.NullCheck(lookupService);
        Assert.NullCheck(userContext);

        _repService = repService;
        _lookupService = lookupService;
        _userContext = userContext;
    }
}


Solution 1:[1]

I think the problem is inheriting form BaseApiController. You should try setting that to Controller.

You should also make your that you specify routing on your controllers. A link to the docs about endpoint routing.

Maybe your endpoints dont have a correct return type, this should be of Type ActionResult

Solution 2:[2]

I found the problem, I had this decorator

[Route("api/[controller]"]

instead of this

[Route("api/[controller]/[action]"]

its working now

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 rbdeenk
Solution 2 Kuro Neko