2

Since the released of Web API, I've been wondering whether it's necessary or helpful to migrate/change MVC controllers to use Web API calls. Since it's hard to describe code I'll show an example of what I mean:

Current controller, the _userService is basically the Business layer of the application that is then calling the repository and getting all users.

[HttpGet]
public ActionResult List()
{
    var list = _userService.GetAll();
    if(list.Any())
        return View("List",list);
    return View(list.ToList());
}

Would it be better to use Web API, and get the list of the users as JSON object and then bind it to the view with some JavaScript? (possibly knockout if applicable).

Not sure if my question is clear enough, so let me know if I have to explain something.

  • I would recommend Web API for more testability/portability, but that does not exclude MVC. – Dmitry Negoda Jul 30 '13 at 15:12
  • Keep on using MVC Controllers whenever you want to respond with a View() and go with Web API for anything that is not dependent on a view! – MaxSC Jul 30 '13 at 15:35
2

What you are asking is not about how to code in MVC or in ASP.NET, I think it's about using a different approach.

The architecture that you kind of describe is very similar to a Single Page Application (SPA) where the server-side turns into a thin API and all the logic of the application is moved to client-side code written in JavaScript (logic, navigation, routing, rendering, etc..).

Of course you could have a "hybrid" solution using MVC to deal with logic, navigation, routing, etc.. and have some API's to serve some data to some pages that use some client-side code.

However, I think this would add complexity to the application because there would be a thick-server (MVC) and in some parts a semi-thick-client plus the fact that there would be an API. So it would end up with MVC + API + Client-Side code.

The SPA approach follows the thin-server / thick-client. The server only acts as a repository of Data + some authentication logic (if needed) and the client has all the logic to run the application in the browser. All the communications between the browser and server use AJAX. This way we reduce the complexity by only using an API + JavaScript.

I don't know if this answer would help you, but I think that if you start to think about using AJAX more and more in MVC, you will end up with a SPA.

  • so based on your response it's not really necessary to use API instead of MVC controller unless the application is approaching a SPA style? – SOfanatic Jul 31 '13 at 15:01
  • 1
    Exacly! ASP.NET MVC allows you to have actions that return JSON or XML, so you could use this actions to request some data using AJAX. I wouldn't mix Web API and MVC unless there's a real need to create an API (mobile app, for example) – margabit Jul 31 '13 at 15:05
  • I am interested in what you feel you lose with Web API versus standard MVC? i.e. with Views / model binding etc. – Matt Evans Jan 24 '14 at 7:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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