Reputation: 13992
I have a bunch of ComboBoxes
in my AngularJS web application that need to be filled. For the front-end, the only data I need is lists of Key, Value pairs.
The problem is for the back-end: in order to fill those lists, I have generated a single Web API endpoint that provides "lists of things".
The route is /api/list/{id}
Depending on the value of {id}
, it will return lists of Users
, Accounts
, Books
, Cars
... in the same of key, value. For example, for the Id=1, it will return a list of Cars:
{1, "Ford" }, {1, "Toyota"}, {3, "Honda" } ...
That will be perfect for filling the ComboBoxes.
So... I have a BIG SWITCH inside the method in my controller to determine which kind list I should return.
I feel this is really dirty for the back-end, although for the front-end is quite handy. Just calling the same endpoint with a different magic number, I will get a different list.
My question: Is there a better and cleaner approach for this scenario?
Upvotes: 0
Views: 192
Reputation: 2983
You can use attribute routing to map an id to a specific action.
First action with ID 1:
[Route("/api/list/1")]
Second action with ID 2:
[Route("/api/list/2")]
Upvotes: 0
Reputation: 966
This would usually be split in Controllers per data type ie. UserController
, AccountController
etc. Then each controller would have its action List
.
Later, if needed, additional actions would be added to each controller (create/delete/etc.) and everything stays clean.
On the frontend you would have change your code just to not send magic number as a parameter but as part of the address:
Upvotes: 1