Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between ApiController and Controller in ASP.NET MVC

I've been playing around with ASP.NET MVC 4 beta and I see two types of controllers now: ApiController and Controller.

I'm little confused at what situations I can choose a particular controller.

For ex: If I want to return a view then I've to use ApiController or the ordinary Controller? I'm aware that the WCF Web API is now integrated with MVC.

Since now we can use both controllers can somebody please point at which situations to go for the corresponding controller.

like image 536
VJAI Avatar asked Feb 29 '12 07:02

VJAI


People also ask

What is difference between controller and ControllerBase?

ControllerBase class Web API controllers should typically derive from ControllerBase rather from Controller. Controller derives from ControllerBase and adds support for views, so it's for handling web pages, not web API requests. If the same controller must support views and web APIs, derive from Controller .

What is the use of ApiController?

The ApiController attribute is commonly coupled with the ControllerBase class to enable REST-specific behavior for controllers, and it allows us to build HTTP APIs. First of all, it provides implicit model state validation, which means that we do not need to explicitly check the ModelState.

What is the difference between ViewResult () and ActionResult () in ASP.NET MVC?

ActionResult is an abstract class, and it's base class for ViewResult class. In MVC framework, it uses ActionResult class to reference the object your action method returns. And invokes ExecuteResult method on it. And ViewResult is an implementation for this abstract class.

What is ApiController in asp net?

Web API Controller is similar to ASP.NET MVC controller. It handles incoming HTTP requests and send response back to the caller. Web API controller is a class which can be created under the Controllers folder or any other folder under your project's root folder.


2 Answers

Use Controller to render your normal views. ApiController action only return data that is serialized and sent to the client.

here is the link

Quote:

Note If you have worked with ASP.NET MVC, then you are already familiar with controllers. They work similarly in Web API, but controllers in Web API derive from the ApiController class instead of Controller class. The first major difference you will notice is that actions on Web API controllers do not return views, they return data.

ApiControllers are specialized in returning data. For example, they take care of transparently serializing the data into the format requested by the client. Also, they follow a different routing scheme by default (as in: mapping URLs to actions), providing a REST-ful API by convention.

You could probably do anything using a Controller instead of an ApiController with the some(?) manual coding. In the end, both controllers build upon the ASP.NET foundation. But having a REST-ful API is such a common requirement today that WebAPI was created to simplify the implementation of a such an API.

It's fairly simple to decide between the two: if you're writing an HTML based web/internet/intranet application - maybe with the occasional AJAX call returning json here and there - stick with MVC/Controller. If you want to provide a data driven/REST-ful interface to a system, go with WebAPI. You can combine both, of course, having an ApiController cater AJAX calls from an MVC page.

To give a real world example: I'm currently working with an ERP system that provides a REST-ful API to its entities. For this API, WebAPI would be a good candidate. At the same time, the ERP system provides a highly AJAX-ified web application that you can use to create queries for the REST-ful API. The web application itself could be implemented as an MVC application, making use of the WebAPI to fetch meta-data etc.

like image 135
Andre Loker Avatar answered Oct 13 '22 11:10

Andre Loker


Which would you rather write and maintain?

ASP.NET MVC

public class TweetsController : Controller {   // GET: /Tweets/   [HttpGet]   public ActionResult Index() {     return Json(Twitter.GetTweets(), JsonRequestBehavior.AllowGet);   } } 

ASP.NET Web API

public class TweetsController : ApiController {   // GET: /Api/Tweets/   public List<Tweet> Get() {     return Twitter.GetTweets();   } } 
like image 41
Manish Jain Avatar answered Oct 13 '22 12:10

Manish Jain