1

I have a route url like http://localhost/p201/info for which I want to choose the controller ProductController. For http://localhost/p1201/info also, I want the same ProductController.

For a route like http://localhost/c301/info, I want to select the CategoryController.

I have a key-value dictionary in the app to choose the controller for a segment:

segment - controller p201 - ProductController p1201 - ProductController c301 - CategoryController

What route extension I have to implement to dynamically select the controller from the key-value dictionary for a segment name and invoke the controller method?

wonderful world
  • 10,969
  • 20
  • 97
  • 194
  • 1
    Do you literally have an `IDictionary` somwehere which maps those values? – Marco May 27 '18 at 15:15
  • Yes. That list is managed in the database, and read into cache in ```startup.cs``` file as a dictionary. – wonderful world May 27 '18 at 15:18
  • 1
    Have you considered using attribute routing – Nkosi May 27 '18 at 18:31
  • I don't know what to specify in the attribute to say the controller maps to ```p201, p1201, c301``` etc... There can be many such segments. – wonderful world May 27 '18 at 21:29
  • 1
    How many different controllers do you have? If its only a small number, you could use a `RouteConstraint` (refer [this answer](https://stackoverflow.com/questions/37358416/routing-in-asp-net-mvc-showing-username-in-url/37359345#37359345) for an example –  May 27 '18 at 22:05

0 Answers0