0

I have an WebApi 2 application which has become popular with our customers and as a result we're aiming to make it as generic as possible so that subtle changes to the application will be available to all but I have a problem.

My plan was to use the brand of each company to determine which database connection string to use and this would allow me to leave the javascript I've written as it is. With this in mind I need to find a way to route my application a little like this:

api/{brand}/{controller}/{id}

Currently it is:

api/{controller}/{id}

Does anyone know of a way to add to the route before {controller} so that it still points to the same controller?

Ash Robinson
  • 41
  • 1
  • 1
  • 9
  • I'm afraid `api/{brand}/{controller}/{id}` is possible. how about `api/{controller}/{brand}/{id}`? Are you looking for a custom `IHttpControllerSelector` implementation? – Amit Kumar Ghosh May 23 '16 at 10:30
  • You are in control of the routes. are you looking to keep the `api/{controller}/{id}` route? – Nkosi May 23 '16 at 10:50
  • @Nkosi api/{controller}/{id} can go entirely and be replaced with api/{brand}/{controller}/{id} it will be the way the application would work if I could get it configured that way. – Ash Robinson May 23 '16 at 10:54
  • @AmitKumarGhosh, that is a possibility but is a much larger change to both the consuming ui and the api than just being able to add a brand prefix. – Ash Robinson May 23 '16 at 10:55
  • 1
    Assuming that this route will apply to multiple controllers in your project, there is nothing stopping you from using that route template. You would however have to make some changes to your api to cater for the `{brand}` parameter in your controllers/actions. Are you using convention-based routing or attribute routing – Nkosi May 23 '16 at 11:04
  • 1
    Attribute routing but I think I've just managed to get there. I've added {brand} before controller on my route mappings and added them to the controller and all seems to be working perfectly now. Thanks @Nkosi! – Ash Robinson May 23 '16 at 11:12
  • @AshRobinson, glad to help. – Nkosi May 23 '16 at 11:16

0 Answers0