• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home ยป Resolved: Is there a way to create route groups in an aspnet controller?

Resolved: Is there a way to create route groups in an aspnet controller?

0
By Isaac Tonny on 03/04/2023 Issue
Share
Facebook Twitter LinkedIn

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
In the code above 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:
And then implement the controllers like so, passing the (injected) dependency down to your base controller:

If you have better answer, please add a comment about this, thank you!

Source: Stackoverflow.com

asp.net-core asp.net-mvc c# model-view-controller
Share. Facebook Twitter LinkedIn

Related Posts

Resolved: How to stop a thread that has a blocking function from easygui in python

05/04/2023

Resolved: Removing null keys from a json array of objects

05/04/2023

Resolved: How can I generate at compile-time a separate OpenAPI Swagger.json file for each Controller in my ASP.NET project?

05/04/2023

Comments are closed.

© 2023 DEVSFIX.COM

Type above and press Enter to search. Press Esc to cancel.