Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

ASP.NET Core REST API

This document provides an overview of creating a REST API using ASP.NET Core, focusing on a controller-based approach. It includes steps for setting up a web project, creating a model class, and scaffolding a controller for managing purchased items. Additionally, it covers testing the API using Swagger and includes a couple of quiz questions related to the content presented.

Uploaded by

Romina González
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

ASP.NET Core REST API

This document provides an overview of creating a REST API using ASP.NET Core, focusing on a controller-based approach. It includes steps for setting up a web project, creating a model class, and scaffolding a controller for managing purchased items. Additionally, it covers testing the API using Swagger and includes a couple of quiz questions related to the content presented.

Uploaded by

Romina González
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

ASP.

NET Core
REST API

#FullStackFuture

© NIIT-StackRoute
© NIIT-StackRoute 2022-2023
2022-2023
Outcome

After going through this video, you will learn


about:
● Overview
● Creating API using Visual Studio
● Test it using swagger

© NIIT-StackRoute 2022-2023
Overview

● ASP.NET Core supports two approaches to creating APIs:


1. a controller-based approach and minimal APIs. Controllers in an API project are
classes that derive from ControllerBase.
2. Minimal APIs define endpoints with logical handlers in lambdas or methods.

● This session covers the basics of building a controller-based web API that uses a
database.

© NIIT-StackRoute 2022-2023
Overview

● In this session we will create following API –

© NIIT-StackRoute 2022-2023
Http Request
Controller

Client Http Response


{Name:”Laptop”}
Read/Write
Serialize Model

Data Access
Layer

© NIIT-StackRoute 2022-2023
Create a web project

● From the File menu, select New > Project.


● Enter Web API in the search box.
● Select the ASP.NET Core Web API template and select Next.
● In the Configure your new project dialog, name the project PurchasedItemsAPI and
select Next.
● In the Additional information dialog:
● Confirm the Framework is .NET 6.0 (Long-term support).
● Confirm the checkbox for Use controllers(uncheck to use minimal APIs) is checked.
● Select Create.

© 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

public static List<PurchasedItem> Items = new List<PurchasedItem>()


{
new PurchasedItem(){ Id=1, Name="Laptop", UnitPrice=89000 },
new PurchasedItem(){ Id=2, Name="Desktop", UnitPrice=59000 },
new PurchasedItem(){ Id=3, Name="Tablet", UnitPrice=49000 }
};

// 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

Q2. Which verb is used to create update method?


a) Post
b) Update
c) Put
d) Change
Ans - c
© NIIT-StackRoute 2022-2023
Recall

Recall

© NIIT-StackRoute 2022-2023
Thank You

#FullStackFuture

© NIIT-StackRoute
© NIIT-StackRoute 2022-20232022-2023

You might also like