-1

Is it possible to call WebAPI endpoints without extending the Controller base class? I have a background service (base class HostedService implementing IHostedService) in .Net Core. My class structure is already set in stone so I can't change the base class of my background services. But it would be a huge help if I could call url endpoints on them without actually having a separate controller.

Is this possible?

EDIT: My background services look exactly like this.

LeonidasFett
  • 3,052
  • 4
  • 46
  • 76
  • Possible duplicate of [Is there any way to use class without inheriting from ApiController, use as api controller?](https://stackoverflow.com/questions/39547709/is-there-any-way-to-use-class-without-inheriting-from-apicontroller-use-as-api) – Marcus Höglund Oct 01 '18 at 20:16
  • @MarcusHöglund That question is about the old ASP.NET MVC, not ASP.NET Core. – poke Oct 01 '18 at 21:37
  • Check this out [ASP.NET Core API Endpoints](https://github.com/ardalis/apiendpoints) – Dmitry Pavlov Feb 05 '21 at 18:46

1 Answers1

1

Why don't you use something similar to ValuesController in the link you provided?

You only have to create a new Controller with almost any logic, just calls to your Hosted service.

Paco Ferre
  • 311
  • 2
  • 7
  • I am already doing it like that. I inject my service into my controller and call methods there. But I need to create a controller just for calling those methods and for nothing else and it would be better if I could do away with the controller entirely. – LeonidasFett Oct 01 '18 at 19:44
  • 1
    But that's how the MVC framework works. You create a controller as a layer to expose your endpoints. That layer does not necessarily have to contain the business logic. So it's perfectly fine to call a different service in your controller. But you *need* a controller to make the API available with MVC. – Note that you do not need to inherit from `Controller` or `ControllerBase`. You just need a `Controller` suffix on the controller's type name. – poke Oct 01 '18 at 21:36