xnr_z
xnr_z

Reputation: 1201

How does the ApiController class work and behave?

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?

Upvotes: 3

Views: 12489

Answers (2)

Tallmaris
Tallmaris

Reputation: 7590

Oh, that's a lot of questions... :)

To answer them:

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"?

ASP.NET Routing uses that Coonvention, so, as far as I know, if you name the class as you like, not following that convention, they will not be recognised and you will most probably get a 404. So to answer your question, yes, the name has to end with Controller, unless you override the default convention (see Mike Goodwin answer).

By following a tutorial I put my ApiController in 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?

The folder does not matter, ASP.NET will look for a class that matches the name. The Api Controllers are recognised as API controllers because they inherit from the base ApiController rather than the Controller one

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?

This is not clear... what do you mean "call" an API? if the API is in another project it will probably be sitting on a separate address... All you need is to direct your JSON request to that address, so if you are using jQuery do this:

$.getJSON('http://other.server/api/Controller', function(data) { ... });

rather than simply this:

$.getJSON('/api/Controller', function(data) { ... });

(Just beware of cross-site scripting problems).

Hope that clears things for you. Feel free to ask if something desnt' make sense...

Upvotes: 6

Mike Goodwin
Mike Goodwin

Reputation: 8880

In ASP.Net MVC, you can have complete control over the controller location and instantiation by implementing a custom controller factory.

This is a class that implements the IControllerFactory interface. This provides methods for selecting and instantiating controllers based on the incoming request context. It also has methods for other controller lifecycle behaviours.

The name convention based behaviours that you see in standard MVC are simply the logic coded into the default controller factory (which is of type DefaultControllerFactory funnily enough)

Here is an example of how to do a customer controller factory:

http://www.mgolchin.net/posts/18/dive-deep-into-mvc-icontrollerfactory

So the answer to your questions is yes, you can do anything you want, but no, it is not all automatic.

Upvotes: 3

Related Questions