0

We are developing an ASP.NET MVC application, and we decide separate our API Services/Controllers inside a folder named for example API_Services instead put them directly in the controllers.

The problem is: how we set/define the route for that? Tipically is like the following code (at App_Start folder and WebApiConfig.cs file) :

routes.MapHttpRoute(
name: "API Default",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }

We try set the routeTemplate like:

routeTemplate: "API_Services/api/{controller}/{id}",

Or this:

routeTemplate: "api/API_Services/{controller}/{id}",

Doesn't work... someone can help us? Thank you!

solrac
  • 629
  • 3
  • 16

3 Answers3

1

If I understand the question right: if you use some web api controllers I would recommend to read the book "ASP.NET Web API 2: Building a REST Service from Start to Finish 2nd Edition By Jamie Kurtz , Brian Wortman". There is the chapter about api versions and the author describes routing through folders (V1 folder, V2 etc). Also You can use just "inline-attribute" routing. And You can read about Areas (or just try to put some "namespaces" into the routing settings or play with it ). I hope it helps, sorry for my English.

Community
  • 1
  • 1
VVildVVolf
  • 136
  • 2
  • 12
0

We are strucure like this:

Controllers (Folder)
    A_Controller
    B_Controller
API (Folder)
    A_Controller
    B_Controller

After a lot of google search, I found this:

Controllers (Folder)    
    A_Controller
    B_Controller
    API (Folder)
        A_Controller
        B_Controller

(which apparently works, but just tomorrow I go can test and then I give feedback. Anyway thanks for other solutions. Thank you!)

solrac
  • 629
  • 3
  • 16
0

The changes you are talking about are to your folders, not the routes, which the conventions will ignore.

The way it works means that whatever project folder you move it into, if it is named XXXController it will be found without route changes. So with the following this:

routes.MapHttpRoute(
name: "API Default",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }

http://yoursite.com/api/XXXController will still find your controller even if it is in another folder:

You can play around with defaults if you want more control but I don't think this is what you are asking for. For example:

routes.MapHttpRoute(
name: "API Default",
routeTemplate: "api/API_services/{id}",
defaults: new { controller="mycontroller" id = RouteParameter.Optional }

So whenever people visit http://yoursite.com/api/API_services, the controller used would be "mycontroller".

Ben Hall
  • 1,353
  • 10
  • 19