0

I want to route all api calls to one controller. Im using MVC and what i would like to do is have dynamic endpoints so basically whatever url you use it will point to one controller which does logic. The thing that makes it more tricky is that i want to know which controller is being called so for example if there is an Get at: "https://{url}/project/id" i want it to point to my "dynamic" controller and have "project" as an parameter(or something that can tell me what endpoint was used) so i know which controller is called.

I know this is not a nice way of setting up an API but currently this seems to best fit our needs with the requirements we have.

[edit] I see ive messed up my question since i was mixing up the MVC controller and ApiController. I ended up using the answer in this thread: Route all Web API requests to one controller method

Blaataap
  • 21
  • 9
  • The routing framework is currently doing it for you. Why do you want to customize it ? What are you trying to do. May be there is another solution to your problem. – Shyju Nov 22 '17 at 17:29
  • If you [subclass `RouteBase`](https://stackoverflow.com/a/31958586/), you can literally program your routes to do whatever you want. You can use [RouteData.DataTokens](https://msdn.microsoft.com/en-us/library/system.web.routing.routedata.datatokens(v=vs.110).aspx) to pass any metadata about what route is currently matched to the rest of your application. – NightOwl888 Nov 22 '17 at 19:58
  • @Shyju The problem is that i want to be able to deliver an Rest API but all business logic is stored in the database even which endpoints are available will be stored in the database and can change, therefor i need the rest api to be dynamic in such way that i can catch the url and make the right database call. NightOwl888 that seems interesting and dynamic enough to do what i want, ill check it out thanks. – Blaataap Nov 23 '17 at 07:58

1 Answers1

0

In Asp.Net MVC you can route e.g to a Custom Error page like this

routes.MapRoute("NotFound", 
                "{*url}", 
                new { controller = "Error", action = "PageNotFound" });

So with a little renaming it looks like

 routes.MapRoute("AllRequests", 
                 "{*url}", 
                 new { controller = "Dynamic", action = "Dynamic" });

Is that what you want?

less
  • 699
  • 6
  • 25
  • It sort of is what i want but instead i would like it to be like: routes.MapRoute("AllRequests", "{*controller*}/{action}/{id}", new { controller = "Dynamic", action = "Index" }); Where the {*controller*} can be anything in the url but will always point to the same controller. In my controller i want to know what is used as {*controller*}. – Blaataap Nov 23 '17 at 07:55
  • And what did you not get with my proposition? After that you get the Request (or URI) in the DynamicController by the HttpContext. – less Nov 23 '17 at 08:26
  • Because the action is being ignored so it always uses the default action i set(in this case Index). The action should still work normally so /{controller}/Details/5 should trigger the right method – Blaataap Nov 23 '17 at 08:45
  • what happens then if I request /controller1/details ? Doesn't it do the same as if I request /controller2/action1. I think if you want to do the routing yourself- you have to do it – less Nov 23 '17 at 08:54