I studied how to use APIs in a MVC project but I have some doubts about. (I'm writing in C# language before anyone asks).
So far, I know that the Api's route configuration is set in the WebApiConfig
class and by default the route is:
routeTemplate: "api/{controller}/{id}"
By doing this, when I compile a jSon request I know what URI I have to call in order to obtain a specific result.
But I'd like to be more specific so I modified the Api's route to:
routeTemplate: "api/{controller}/{action}/{id}"
With this route I will be able to build an URI directly to a specific action (method?) inside my ApiController.
Also I learned that the /{controller}/
, while building the URI with jSon, is the name of the class. That is, if the class is ProductsController
the controller name that I have to use to build the URI is just /products
. (So the whole URL will be /api/products
).
Here questions are:
If I have an ApiController class named just Products
, is it recognizable as part of an URI? Or the ApiController class have to end with "Controller"?
By following a tutorial I put my ApiController il the same folder as other Controllers. I know that is possible to put Apis into different folders. So, is every single API automatically recognized by MVC? I mean, wherever I save them, are they recognized as API?
If so, could I eventually call an API located in a different project than the one I'm working on?
Can I create a single project (as class library) with a collection of APIs?
Does the route configuration change if I want to call an API in a distinct project?