To generate controllers for the models, right-click on Controllers folder | Add | Controller | API Controller with actions, using Entity Framework.
First, let's start with ProductsdetailsController, as we want to show the products list to our customers initially.
The Productsdetail.cs model class generated through scaffolding should look like the following snippet:
public partial class Productsdetail
{
public Guid Id { get; set; }
public Guid? Productid { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string Url { get; set; }
public int Views { get; set; }
public Products Product { get; set; }
}
The preceding code can also be used to generate the controller with GET, POST, PUT, and DELETE action methods: (We're focusing on the GetProductsdetail method for now.)
// GET: api/Productsdetails
[HttpGet]
public IEnumerable<Productsdetail> GetProductsdetail()
{
return _context.Productsdetail;
}
You can quickly test whether your controller is working using Postman, as shown in the following screenshot:
Here, the URL is http://localhost:57571/api/Productsdetails and the type is GET. We can see the results in the result box, which shows us an array of product details in a JSON format. Note that we sent this request by setting the contentType header with the value application/json inside the request's Header tab.