ASP.NET Core REST API
ASP.NET Core REST API
NET Core
REST API
#FullStackFuture
© NIIT-StackRoute
© NIIT-StackRoute 2022-2023
2022-2023
Outcome
© NIIT-StackRoute 2022-2023
Overview
● This session covers the basics of building a controller-based web API that uses a
database.
© NIIT-StackRoute 2022-2023
Overview
© NIIT-StackRoute 2022-2023
Http Request
Controller
Data Access
Layer
© NIIT-StackRoute 2022-2023
Create a web project
© NIIT-StackRoute 2022-2023
Create a web project
© NIIT-StackRoute 2022-2023
Create a web project
© NIIT-StackRoute 2022-2023
Create Model class
● A model is a set of classes that represent the data that the app manages.
● The model for this app is the PurchasedItem class.
namespace PurchasedItemsAPI.Models
{
public class PurchasedItem
{
public int Id { get; set; }
public string Name { get; set; }
public int UnitPrice { get; set; }
}
}
© NIIT-StackRoute 2022-2023
Scaffold a controller
© NIIT-StackRoute 2022-2023
Scaffold a controller
using Microsoft.AspNetCore.Mvc;
using PurchasedItemsAPI.Models;
// For more information on enabling Web API for empty projects, visit
https://go.microsoft.com/fwlink/?LinkID=397860
namespace PurchasedItemsAPI.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class PurchasedItemsController : ControllerBase
{
© NIIT-StackRoute 2022-2023
Scaffold a controller
// GET: api/<PurchasedItems>
[HttpGet]
public List<PurchasedItem> Get()
{
return Items;
}
}
} © NIIT-StackRoute 2022-2023
Run and Test API
© NIIT-StackRoute 2022-2023
Just a Minute
Q1. Which API is created by default when we create API project?
a) StockRates
b) WeatherForecast
c) RainForecase
d) TemperatureForecast
Ans - b
Recall
© NIIT-StackRoute 2022-2023
Thank You
#FullStackFuture
© NIIT-StackRoute
© NIIT-StackRoute 2022-20232022-2023