Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why people use Web API controller in MVC [closed]

i am new in MVC & web api.so i have couple of question regarding MVC & web api usage.

1) i have seen that people using Web API controller in MVC instead of MVC default controller. i like to know the reason and advantage.

2) also guide me about when we should use MVC default controller and when we should use Web API controller in MVC instead of MVC default controller ?

3) what are the restriction is there when we work with Web API controller .

4) can we declare any public & private method in Web API controller with any function name instead of get() put() delete() etc.

5) what is put() function for ?

6) can we override any function in Web API controller ?

thanks

like image 504
Thomas Avatar asked Nov 12 '13 08:11

Thomas


3 Answers

Web API and ASP.NET MVC are two different beasts.

ASP.NET MVC is used for a RPC style of app, this means that your endpoints perform arbitrary actions. So you can have a PersonController with an action called FooBar and you can call that procedure by GETting domain.com/person/foobar.

Web API is used (mostly) for REST (although is also does support RPC). This means that your controller exposes a resource that you then GET or POST or PUT or DELETE. So your PersonController in Web API will have a Get method and a Post method etc.

You can declare any methods you like in an API controller class. In a 100% purely REST controller you would have methods that map to the HTTP verbs (i.e. Get, Post, Put, Delete). You can use the Web API routing mechanism to also map URLs to an action (see more here: http://encosia.com/rest-vs-rpc-in-asp-net-web-api-who-cares-it-does-both/) if you'd like too.

The put-function is for handling the HTTP verb PUT. So if you have a PersonController with a Put method you would in that method e.g. handle an update of a person instance.

like image 103
Jacob Rohde Avatar answered Nov 18 '22 12:11

Jacob Rohde


1) With a WebAPI controller you can just return json/xml, resulting in an advantage: you can use any client, not just a web browser. You also get GET ajax by default in WebAPI.

2) Are you only going to use a web browser? Use MVC. Otherwise? Use WebAPI and a web browser/whatever as a client to WebAPI.

3) returning json/xml.

4) Yes.

5) Update an object.

6) Yes.

like image 25
danielrozo Avatar answered Nov 18 '22 10:11

danielrozo


I think you have to try following url link for further understanding about Web API. It will solved all your doubt.

http://www.asp.net/web-api

like image 3
Vaibhav Parmar Avatar answered Nov 18 '22 10:11

Vaibhav Parmar