MANOJ VARMA
MANOJ VARMA

Reputation: 49

Can we completely replace the functionality of MVC with api

I have recently started learning both .Core MVC and WEB API. I found that functionality of Web Api is similar to MVC,then why can't we use API instead of MVC for all cases of MVC

For example for returning a list of Pies from DbContext _dbcontext Code:

Public IAction Index()
{
    var PiesCollection=_dbcontext.Pies.ToList();
    return View(PiesCollection);
}

Instead of returning the PiesCollection to a View,why can't we use AJAX from a view to call GetPies API and replace it with

public IAction GetPies()
{
    return JsonResult(_dbcontext.Pies.ToList());
}

Upvotes: 0

Views: 82

Answers (1)

Justin
Justin

Reputation: 15

There's nothing stopping you from doing that, however, it comes with the downside of putting more work on the front end, and can cause some irritating side effects for users if you are not careful.

Rather than doing all the work building the HTML before sending it to the browser, you are sending them incomplete HTML with spaces left open for 'future' content. Then the page then has to request this missing content. The service has to do all the same work as before gathering the data, but now has to serialize it to send to the browser. Then the browser has to parse that content to build a the UI.

This can mean that the front-end is no longer coupled to your data so you can cache it or host it on other servers since it doesn't need all the logic in it. This is how some mobile apps work even. The front end isn't a website anymore, but an iOS or Android app, that gets all the data from the asp.net services.

But the downside is that if the UI is heavily driven by the data returned by that service, then your user has to wait for the browser/app to get the response, parse it, and render it onto the screen. This can be extra irritating for users when the contents of the page move or change as data loads in.

Upvotes: 1

Related Questions