Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question

3 Answers

up vote 54 down vote accepted

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

http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api

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.

share|improve this answer
Note: since your data will be sent over the wire, how will it be formatted? The way data that an ApiController returns is formatted is determined by content negotiation, and GlobalConfiguration.Configuration.Formatters... link: blogs.msdn.com/b/kiranchalla/archive/2012/02/25/… – Tim Lovell-Smith Dec 17 '12 at 20:02

Use the Controller, not the ApiController.

As for the differences: http://encosia.com/asp-net-web-api-vs-asp-net-mvc-apis/ - Mike Wasson

A beautiful synopsis:

Content negotiation

Flexibility

Separation of concerns

share|improve this answer
I'm not sure if your first line is a typo but the article you link to appears to me to be about the advantages to ApiController (see also the code post by Manish Jain in this question from the same article). – grantnz Jun 4 at 2:33

Check this link (Thanks to Dave Ward!!):

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();
  }
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.