O'Reilly logo

Continuous Integration, Delivery, and Deployment by Sander Rossel

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Selecting the data

We are now ready to get to work to replace our repository methods to use AJAX requests to fetch the data from the backend. Let's start with the top three products on the home page. First, we must decide where to put our GetTopProducts function. Since the products are being shown on the home page, HomeController seems like a good spot. The function itself is pretty easy, especially compared to the JavaScript MongoDB callbacks:

public IActionResult GetTopProducts(){   using (var context = new WebShopContext())   {      var products = context.Products.Take(3).ToList();      return Json(products);   }}

That is really all there is to it. These three lines of code open up a connection to the database, generate a SQL query that is roughly equivalent ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required