Pregunta

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.

¿Fue útil?

Solución

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.

Licenciado bajo: CC-BY-SA con atribución
scroll top