In this post, we will see how to resolve Is there a way to create route groups in an aspnet controller?
Question:
I want to know if there’s a way to create subclasses that act as a routing group for an api using AspNetCore Mvc.These are the routes I need to use.
api/speak
api/dance/tango
api/dance/salsa
api/sing/song
api/sing/tune
myService
doesn’t work in the subclasses. I used that code as I felt it explained best what I want to accomplish. (I had the subclasses inherit ApiController
so that I could use myService
inside those methods but that didn’t fix the routes not working.)The reason I’m on stack overflow is because I can’t get this to work, and I can’t find anything online.
I’m using Swagger to test the routes, that’s how I can see that they’re not showing up.
All help is greatly appreciated.
Best Answer:
These would be separate controllers. There’s a good argument for doing it this way (idiomatically), anyone with experience would instantly recognise the pattern and know exactly how things work.You would use a base controller to capture the service dependency:
If you have better answer, please add a comment about this, thank you!
Source: Stackoverflow.com