How does the ApiController class work and behave?

xnr_z

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?

Tallmaris

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...

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How does the interface in anonymous inner class work?

From Dev

Why does "include" behave differently in the global context than it does in a class?

From Dev

Exactly how does this class variable work?

From Dev

How exactly does Android debugging work and why does my app behave differently when debugging it?

From Dev

How does Ruby modules lookup path behave?

From Dev

How does 'rate' property of JavaFX Animation class behave?

From Dev

How Protected Class will behave in c#

From Dev

How does util:list behave with scope="prototype"?

From Dev

How does `try` behave in Parsec?

From Dev

How does the Java class loading work

From Dev

Does Behave (BDD) work with Python 3.4?

From Dev

Getting an ApiController to work with areas?

From Dev

Implementing custom option parser into behave does not work

From Dev

How does [ ] work on a class in Ruby

From Dev

How does this implementation of std::is_class work?

From Dev

How does geofences behave when there are overlapping CLRegions

From Dev

How does the compilation of a class work?

From Dev

How does `<$` function work in the Functor class?

From Dev

How does the C# Random class work

From Dev

How does a "stand alone" class file work?

From Dev

How does class object relation work?

From Dev

How does the nested class with override work?

From Dev

How exactly does the AudioRecord class work?

From Dev

Why does "include" behave differently in the global context than it does in a class?

From Dev

How exactly does Android debugging work and why does my app behave differently when debugging it?

From Dev

How does logical operators behave?

From Dev

Implementing custom option parser into behave does not work

From Dev

How does this recursive construction of a class work?

From Dev

How does this javascript class and method work?

Related Related

  1. 1

    How does the interface in anonymous inner class work?

  2. 2

    Why does "include" behave differently in the global context than it does in a class?

  3. 3

    Exactly how does this class variable work?

  4. 4

    How exactly does Android debugging work and why does my app behave differently when debugging it?

  5. 5

    How does Ruby modules lookup path behave?

  6. 6

    How does 'rate' property of JavaFX Animation class behave?

  7. 7

    How Protected Class will behave in c#

  8. 8

    How does util:list behave with scope="prototype"?

  9. 9

    How does `try` behave in Parsec?

  10. 10

    How does the Java class loading work

  11. 11

    Does Behave (BDD) work with Python 3.4?

  12. 12

    Getting an ApiController to work with areas?

  13. 13

    Implementing custom option parser into behave does not work

  14. 14

    How does [ ] work on a class in Ruby

  15. 15

    How does this implementation of std::is_class work?

  16. 16

    How does geofences behave when there are overlapping CLRegions

  17. 17

    How does the compilation of a class work?

  18. 18

    How does `<$` function work in the Functor class?

  19. 19

    How does the C# Random class work

  20. 20

    How does a "stand alone" class file work?

  21. 21

    How does class object relation work?

  22. 22

    How does the nested class with override work?

  23. 23

    How exactly does the AudioRecord class work?

  24. 24

    Why does "include" behave differently in the global context than it does in a class?

  25. 25

    How exactly does Android debugging work and why does my app behave differently when debugging it?

  26. 26

    How does logical operators behave?

  27. 27

    Implementing custom option parser into behave does not work

  28. 28

    How does this recursive construction of a class work?

  29. 29

    How does this javascript class and method work?

HotTag

Archive