Content

  • GET and POST Methods using List
  • PUT and DELETE Methods using List
  • CRUD opearations using EF Core

GET and POST Methods using List

using API_Sessions.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace API_Sessions.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ProductController : ControllerBase
    {
        public static List products = new List
        { 
            new Product{ Id=1, Name="Laptop",Price=40000 },
            new Product{ Id=2, Name="Mouse", Price=500},
            new Product{ Id=3, Name="Keyboard", Price=999},
            new Product{ Id=4, Name="Printer", Price=15000},

        };

        [HttpGet]
        public IEnumerable Get()
        {
            return products;
        }

        [HttpPost]
        public void Post([FromBody]Product product)
        {
            products.Add(product);
        }
    }
}
  • [FromBody] tells the Web API framework to read the value of the parameter from the body of the HTTP request.

  • While submitting POST - select raw and JSON under body tab

Example: body for POST request

{
    "Id":5,
    "Name":"laptop battery",
    "Price":"1200"
}

PUT and DELETE Methods using List

[HttpPut("{id}")]
public void Put(int id, [FromBody] Product product)
{
    products[id] = product;
}

[HttpDelete("{id}")]
public void Delete(int id)
{
    products.RemoveAt(id);
}
  • select PUT(edit body also)/DELETE and send id in url before submitting request


CRUD opearations using EF Core

Steps

  • Install EF Core packages
  • Creating ApplicationDbContext class
  • Set up connectionString in appSettings.json
  • Pass connectionString to ApplicationDbContext through program.cs
  • Add DbSet in ApplicationDbContext
  • Perform Database Migration
  • Create API Controller and update code with EF core methods

Example:

using API_Sessions.Data;
using API_Sessions.Models;
using Microsoft.AspNetCore.Mvc;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace API_Sessions.Controllers
{
    
    [Route("api/[controller]")]
    [ApiController]
    public class ProductDBController : ControllerBase
    {
        private readonly ApplicationDbContext _context;

        public ProductDBController(ApplicationDbContext context)
        {
            _context = context;
        }

        // GET: api/<ProductDBController>
        [HttpGet]
        public IEnumerable<Product> Get()
        {
            return _context.Products.ToList();
        }

        // GET api/<ProductDBController>/5
        [HttpGet("{id}")]
        public Product Get(int id)
        {
            return _context.Products.Find(id);
        }

        // POST api/<ProductDBController>
        [HttpPost]
        public void Post([FromBody] Product product)
        {
            _context.Products.Add(product);
            _context.SaveChanges();
        }

        // PUT api/<ProductDBController>/5
        [HttpPut("{id}")]
        public void Put(int id, [FromBody] Product product)
        {
            var p = _context.Products.Find(id);
            p.Name = product.Name;
            p.Price = product.Price;
            _context.SaveChanges();

        }

        // DELETE api/<ProductDBController>/5
        [HttpDelete("{id}")]
        public void Delete(int id)
        {
            var p = _context.Products.Find(id);
            _context.Products.Remove(p);
            _context.SaveChanges();

        }
    }
}