Monday, 28 March 2016

Difference between ApiController and Controller in ASP.NET MVC

1)    You can use controller to render your normal views only.
But using the ApiController action only returns the data which is serialized and sent to client. So, that you can use it in any Html based application.
2)    You can self-hosting using ApiController but not from MVC Controllers.
3)    If you are aware with ASP.NET MVC then you are already knows about the controllers.
And the APIControllers are same as MVC controllers, but it inherits the ApiController class instead of the Controller class.
4)    APIControllers is a lightweight architecture excepting the web Apps. 
5)    MVC Controller shows the URL Samples matching the default route pattern"{controller}/{action}/{id}". and the ApiController shows "api/{controller}/{id}".
6)    Web API Supports the self hosting, content (you can send content to the client in variety of formats such as images or files) negotiation where the MVC doesn't support it.
7)    Use Controller when - If you're writing an HTML based web/internet application (with the occasional AJAX call returning json here and there).
Use ApiControllers When - If you want to provide a data driven/REST-ful interface to a system.

So, you have to use the API which is compatible with the browsers and all the modern devices apps.
Actually, the Web API is a open source platform for building a REST-ful services over the .Net Framework.   

No comments:

Post a Comment