Sign up ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

I'm building an ASP.NET MVC application that is client-script heavy, it will use JSON and jQuery to manipulate the DOM.

My understanding is both Web API Controller and MVC Controller can return JSON.

Given my scenario, should I use a Web API Controller or an MVC Controller?

share|improve this question
    

6 Answers 6

up vote 122 down vote accepted

Web API Controllers can be created and hosted in any ASP.NET Application, not just MVC applications. Thus, an obvious reason to create a Web API is if you do not have an MVC front-end (e.g. classic, RESTful web-services hosted by your company/organization.)

MVC Controllers typically rely on the MVC Framework, if you look at default templates and most of the work done by the community and your peers you will notice that almost all MVC Controllers are implemented with the View in mind.

Personally, I use MVC Controllers when I intend to respond with a View(), and I'll use a Web API for anything that isn't dependent on a particular view.

There are caveats, of course, but generally speaking if you don't require the Model Binding behavior of MVC, your service is data-centric, and operations are Data-centric (e.g. CRUD operations) then you likely want a 'Web API Controller' instead of a 'Model-View Controller'. Conversely, if your operations are View-centric (e.g. delivering a user admin page to the user), or you need MVC's Model Binding to generate 'ajax partials' (very unlikely), then you will want an MVC Controller instead.

Personally, I use Web API controllers for driving JSON-based RESTful clients, I use MVC controllers for handling basic browser routing and delivery of the SPA.

share|improve this answer

WebAPI is for making an API. If you want someone to be able to consume your API in XML, JSON, etc. You can make a web api.

In your case you only need to talk to client in JSON.

Even though your website is mostly client script driven you would still be using ASP.NET MVC Controller right? And since you may have already logically divided your controllers based on entities then it make sense to add those json serving methods in it as opposed to making another class specifically for web api.

So for your particular situation (if i understand correctly), I would stick with Controllers.

share|improve this answer
    
Thanks, is there a difference in how we create WebAPI vs Controller? – Myagdi Apr 27 '12 at 7:24
1  
@flybyte yes you need to derive from ApiController, see asp.net/web-api/overview/getting-started-with-aspnet-web-api/… – Hasan Khan Apr 27 '12 at 7:44
4  
Web Api can do JSON, as well as the other methods you list. Controllers cant (neatly) be turned into an API, So given the user is having the foresight to ask - I'd suggest using the more scalable/flexible solution. Its not as if its like old school WCF services, web api is generally both powerfull and flexible. So while you only need simple scenarios it stays out of your way. BUt you've got the power should you need it – steve Sep 15 '13 at 20:49

The answer boils down to separation of concerns, fasten the creation of services and to rely on convention rather than configuration.

Controllers main responsibility is to work as a coordinator between view and your model but where as API's main responsibility is to work on data. In the case of API's conventions make it really easy to perform CRUD operations. Below is the mapping between CRUD operation and HTTP actions

  • GET : Read
  • POST: Create
  • PUT: Update
  • DELETE: Delete

So with API's you do not have to create separate actions and attribute them with HTTP actions.

share|improve this answer

The best reason for use of web.API is when you are sure you don't need to return views like mvc can do. Web.API main purpose is sending back data. Sending back a view is something different but if you use plain html with JavaScript for that purpose like you do with angular then web.API is a perfect fit.

share|improve this answer
    
Funny that it took so long (about 3 years) for somebody to come up speaking about AngularJS. I agree with this answer: ASP.Net MVC goes hand in glove with JQuery. ASP.Net WEB API (or any other web-service) goes hand in glove with AngularJS. – Luis Gouveia Sep 14 at 15:59

The only concern I have with ApiController is that it is site-based not area-based. One site can only have one apicontroller subfolder for you to name your controller methods. There are situations you might want to duplicate controller name in different areas :

domain.com/api/area1/controller1/

domain.com/api/area2/controller1/

I remember there are some custom code settings to be able to do this but it does not work by default.

share|improve this answer
    
this seems like a comment, not an answer. – Dylan Hayes Feb 25 at 15:43
    
Don't no really what you are saying. If you name a controller Area1XController then you can do: domain.com/Area1X/1, create a controller: Area2XController and then access it with: domain.com/Area2X/1. The big question is why you want to do that anyway. Area name is abstract it says nothing to a user. If you have lets say 4 Areas then its better to use the functional purpose name for it. – Herman Van Der Blom Apr 18 at 11:32

When I develop small MVC applications I use the controllers to divide application parts in Subsystems. So I look what are the subsystem parts of my program. Let say I have User functions and SubSystemA, SubSystemB etc. then I put those in seperate Controllers. So UserController, SubSystemAController and SubSystemB controller. I dont use repositories then because its a small system and thats the perfect fit. In those Controllers I mix Actions that return both View and Json data. Because a Controller can return View and Json data thats allright. No need for WebApi then. I think whats already said the main reason for WebApi is that you can use it for other things then Mvc. My goal is Always to use less not more. If I would use WebApi that would add another way of implementing code while it does not offer much more. The same thing counts with Script libraries. I use jQuery, jQueryUI, jqGrid and select2. Dont overengineer if its not necessary.

share|improve this answer
    
You've given an example of when you prefer to use MVC, which I appreciate, but in what situation would you prefer to use webapi? – John Henckel Apr 16 at 13:43
1  
That's funny, it just happens I implemented web.API in an existing ASP.net application. The developer implemented all pages as html with JavaScript but had problems with communication to the backend and misused a service for that and blank aspx pages. So when you have an existing ASP.net application and want to mix it with plain html/JavaScript pages that's good possible with web.API and so you get best of both worlds – Herman Van Der Blom Apr 18 at 11:16

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.