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

3
  • 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. Commented Nov 22, 2017 at 17:29
  • If you subclass RouteBase, you can literally program your routes to do whatever you want. You can use RouteData.DataTokens to pass any metadata about what route is currently matched to the rest of your application. Commented Nov 22, 2017 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. Commented Nov 23, 2017 at 7:58

1 Answer 1

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?

Sign up to request clarification or add additional context in comments.

4 Comments

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}.
And what did you not get with my proposition? After that you get the Request (or URI) in the DynamicController by the HttpContext.
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
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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.