KEMBAR78
Basics Of Introduction to ASP.NET Core.pptx
Basics Of Introduction to Web
API With Examples
in C#
Presenter: Rajeev Mishra
1.1 What is a Web API?
• A Web API (Application Programming Interface) is a set of HTTP-based services that allows
applications to communicate over the internet.
• It provides data access to clients such as web apps, mobile apps, and other services.
• Example:
• A mobile banking app calls an API to fetch account balance details.
• A weather application uses an API to fetch live temperature updates.
• Comparison with Other API Types:
API Type Communication Method Used For
Web API HTTP(S) Web & Mobile Apps
REST API HTTP + REST Principles
Cloud Services &
Distributed Systems
SOAP API XML over HTTP Enterprise Applications
1.2 Understanding RESTful APIs
• REST (Representational State Transfer) is an architectural style that follows principles such as:
• Statelessness Each request is independent and contains all required data.
→
• Client-Server Separation Frontend (Client) and Backend (Server) work independently.
→
• Uniform Interface Standardized endpoints and methods (GET, POST, PUT, DELETE).
→
• Resource-Based Design Everything is treated as a resource (/products, /orders).
→
• Example of REST API URLs:
• GET /api/products Fetch all products
→
• GET /api/products/1 Fetch a product by ID
→
• POST /api/products Add a new product
→
• PUT /api/products/1 Update product details
→
• DELETE /api/products/1 Remove a product
→
1.3 Why ASP.NET Core Web API?
• Key Benefits of ASP.NET Core Web API:
• Cross-Platform Works on Windows, Linux, and macOS.
→
• Lightweight & High Performance Uses Kestrel web server for speed.
→
• Built-in Dependency Injection (DI) Helps in managing services efficiently.
→
• Open-Source & Free Community-driven improvements.
→
• Security Features Supports authentication and authorization using JWT, OAuth, Identity Server.
→
1.4 RESTful API and
HTTP Methods
• REST (Representational State Transfer)
is an architectural style for designing
APIs.
• Uses standard HTTP methods:
HTTP
Method
Description Example
GET Fetch data /api/products
POST
Create a new
resource
/api/products
(with body)
PUT
Update an existing
resource
/api/
products/1
DELETE Remove a resource
/api/
products/1
1.5 Setting Up an ASP.NET Core Web API Project
• Using .NET CLI:
• dotnet new webapi -n MyWebAPI
• cd MyWebAPI
• dotnet run
• Using Visual Studio:
• Open Visual Studio.
• Click Create a new project Select ASP
.NET Core Web API.
→
• Configure the project and click Create.
1.6 Understanding the Project Structure
• Folders & Files in ASP.NET Core Web API:
• Program.cs Configures the app and starts the web server.
→
• Controllers/ Contains API controllers (e.g., ProductController.cs).
→
• Models/ Defines data structures.
→
• appsettings.json Stores configuration like database connection.
→
• Properties/launchSettings.json Defines how the app runs.
→
1.7 Creating a Simple API Controller
 namespace EmployeeAdminPortal.Controllers
 {
 // route url - localhost:xxxx/api/employees
 [Route("api/[controller]")]
 [ApiController]
 public class EmployeesController : ControllerBase
 {
 private readonly ApplicationDbContext dbContext;
 public EmployeesController(ApplicationDbContext dbContext)
 {
 this.dbContext = dbContext;
 }
 [HttpGet]
 public IActionResult GetAllEmployees()
 {
 var allEmployees = dbContext.Employees.ToList();
 return Ok(allEmployees);
 }
1.8 Creating a Model (Data Structure)
 namespace EmployeeAdminPortal.Models.Entitites
 {
 public class Employee
 {
 public Guid Id { get; set; }
 public required string Name { get; set; }
 public required string Email { get; set; }
 public string? Phone { get; set; }
 public decimal Salary { get; set; }
 }
 }
1.9 Creating Migrations Folder (Database Schema
Changes)
 namespace EmployeeAdminPortal.Migrations
 {migrationBuilder.CreateTable(
 name: "Employees",
 columns: table => new
 {
 Id = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
 Name = table.Column<string>(type: "nvarchar(max)", nullable: false),
 Email = table.Column<string>(type: "nvarchar(max)", nullable: false),
 Phone = table.Column<string>(type: "nvarchar(max)", nullable: true),
 Salary = table.Column<decimal>(type: "decimal(18,2)", nullable: false)
 },
 constraints: table =>
 {
 table.PrimaryKey("PK_Employees", x => x.Id);
 });
 }
 }
1.10 Creating a DTOs (Data Transfer Objects) class
 namespace EmployeeAdminPortal.Models
 {
 public class AddEmployeeDto
 {
 public required string Name { get; set; }
 public required string Email { get; set; }
 public string? Phone { get; set; }
 public decimal Salary { get; set; }
 }
 }
1.11 Program.cs (Entry Point) 🏁
 using EmployeeAdminPortal.Data;
 using Microsoft.EntityFrameworkCore;
 var builder = WebApplication.CreateBuilder(args);
 builder.Services.AddControllers();
 // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
 builder.Services.AddEndpointsApiExplorer();
 builder.Services.AddSwaggerGen();
 builder.Services.AddDbContext<ApplicationDbContext>(options =>
 options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));
 var app = builder.Build();
 if (app.Environment.IsDevelopment())
 {
 app.UseSwagger();
 app.UseSwaggerUI();
 }
 app.UseHttpsRedirection();
 app.UseAuthorization();
 app.MapControllers();
 app.Run();
1.12 appsettings.json (Configuration File) ⚙️
 This file stores the database connection string and logging settings.
 Key Terms & Explanation:
 "ConnectionStrings": { "DefaultConnection": "Server=LAPTOP-JI9JVS93
SQLEXPRESS02;Database=EmployeesDb;Trusted_connection=true;TrustServerCertific
ate=true;" }
 Defines the SQL Server connection string.
 Trusted_connection=true; → Uses Windows Authentication.
 TrustServerCertificate=true; → Accepts self-signed certificates.
1.13 Database in SQL Server Management Studio
(SSMS) 💾
 The Employees table is created in EmployeesDb.
 The table contains actual employee records.
 You can run SQL queries like:
 SELECT * FROM Employees;
1.14 Swagger UI (API Testing) 📜
 Swagger provides an interactive API documentation.
 ️
🛠️ GET /api/employees → Lists all employees.
 🛠️ POST /api/employees → Adds a new employee.
 Each API call in Swagger shows:
 Request Body
 Response Status (200 OK, 400 Bad Request, etc.)
 Response Body (JSON data)
1.15 Summary
Component Purpose
Program.cs Configures API, middleware, and DB connection.
appsettings.json Stores DB connection and logging settings.
Models (Employee, DTOs) Defines data structures.
Controllers (EmployeesController) Handles HTTP requests.
Data (ApplicationDbContext) Connects to the database using EF Core.
Migrations Defines database schema changes.
Swagger UI Provides API documentation and testing.
Thank You

Basics Of Introduction to ASP.NET Core.pptx

  • 1.
    Basics Of Introductionto Web API With Examples in C# Presenter: Rajeev Mishra
  • 2.
    1.1 What isa Web API? • A Web API (Application Programming Interface) is a set of HTTP-based services that allows applications to communicate over the internet. • It provides data access to clients such as web apps, mobile apps, and other services. • Example: • A mobile banking app calls an API to fetch account balance details. • A weather application uses an API to fetch live temperature updates. • Comparison with Other API Types: API Type Communication Method Used For Web API HTTP(S) Web & Mobile Apps REST API HTTP + REST Principles Cloud Services & Distributed Systems SOAP API XML over HTTP Enterprise Applications
  • 3.
    1.2 Understanding RESTfulAPIs • REST (Representational State Transfer) is an architectural style that follows principles such as: • Statelessness Each request is independent and contains all required data. → • Client-Server Separation Frontend (Client) and Backend (Server) work independently. → • Uniform Interface Standardized endpoints and methods (GET, POST, PUT, DELETE). → • Resource-Based Design Everything is treated as a resource (/products, /orders). → • Example of REST API URLs: • GET /api/products Fetch all products → • GET /api/products/1 Fetch a product by ID → • POST /api/products Add a new product → • PUT /api/products/1 Update product details → • DELETE /api/products/1 Remove a product →
  • 4.
    1.3 Why ASP.NETCore Web API? • Key Benefits of ASP.NET Core Web API: • Cross-Platform Works on Windows, Linux, and macOS. → • Lightweight & High Performance Uses Kestrel web server for speed. → • Built-in Dependency Injection (DI) Helps in managing services efficiently. → • Open-Source & Free Community-driven improvements. → • Security Features Supports authentication and authorization using JWT, OAuth, Identity Server. →
  • 5.
    1.4 RESTful APIand HTTP Methods • REST (Representational State Transfer) is an architectural style for designing APIs. • Uses standard HTTP methods: HTTP Method Description Example GET Fetch data /api/products POST Create a new resource /api/products (with body) PUT Update an existing resource /api/ products/1 DELETE Remove a resource /api/ products/1
  • 6.
    1.5 Setting Upan ASP.NET Core Web API Project • Using .NET CLI: • dotnet new webapi -n MyWebAPI • cd MyWebAPI • dotnet run • Using Visual Studio: • Open Visual Studio. • Click Create a new project Select ASP .NET Core Web API. → • Configure the project and click Create.
  • 7.
    1.6 Understanding theProject Structure • Folders & Files in ASP.NET Core Web API: • Program.cs Configures the app and starts the web server. → • Controllers/ Contains API controllers (e.g., ProductController.cs). → • Models/ Defines data structures. → • appsettings.json Stores configuration like database connection. → • Properties/launchSettings.json Defines how the app runs. →
  • 8.
    1.7 Creating aSimple API Controller  namespace EmployeeAdminPortal.Controllers  {  // route url - localhost:xxxx/api/employees  [Route("api/[controller]")]  [ApiController]  public class EmployeesController : ControllerBase  {  private readonly ApplicationDbContext dbContext;  public EmployeesController(ApplicationDbContext dbContext)  {  this.dbContext = dbContext;  }  [HttpGet]  public IActionResult GetAllEmployees()  {  var allEmployees = dbContext.Employees.ToList();  return Ok(allEmployees);  }
  • 9.
    1.8 Creating aModel (Data Structure)  namespace EmployeeAdminPortal.Models.Entitites  {  public class Employee  {  public Guid Id { get; set; }  public required string Name { get; set; }  public required string Email { get; set; }  public string? Phone { get; set; }  public decimal Salary { get; set; }  }  }
  • 10.
    1.9 Creating MigrationsFolder (Database Schema Changes)  namespace EmployeeAdminPortal.Migrations  {migrationBuilder.CreateTable(  name: "Employees",  columns: table => new  {  Id = table.Column<Guid>(type: "uniqueidentifier", nullable: false),  Name = table.Column<string>(type: "nvarchar(max)", nullable: false),  Email = table.Column<string>(type: "nvarchar(max)", nullable: false),  Phone = table.Column<string>(type: "nvarchar(max)", nullable: true),  Salary = table.Column<decimal>(type: "decimal(18,2)", nullable: false)  },  constraints: table =>  {  table.PrimaryKey("PK_Employees", x => x.Id);  });  }  }
  • 11.
    1.10 Creating aDTOs (Data Transfer Objects) class  namespace EmployeeAdminPortal.Models  {  public class AddEmployeeDto  {  public required string Name { get; set; }  public required string Email { get; set; }  public string? Phone { get; set; }  public decimal Salary { get; set; }  }  }
  • 12.
    1.11 Program.cs (EntryPoint) 🏁  using EmployeeAdminPortal.Data;  using Microsoft.EntityFrameworkCore;  var builder = WebApplication.CreateBuilder(args);  builder.Services.AddControllers();  // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle  builder.Services.AddEndpointsApiExplorer();  builder.Services.AddSwaggerGen();  builder.Services.AddDbContext<ApplicationDbContext>(options =>  options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));  var app = builder.Build();  if (app.Environment.IsDevelopment())  {  app.UseSwagger();  app.UseSwaggerUI();  }  app.UseHttpsRedirection();  app.UseAuthorization();  app.MapControllers();  app.Run();
  • 13.
    1.12 appsettings.json (ConfigurationFile) ⚙️  This file stores the database connection string and logging settings.  Key Terms & Explanation:  "ConnectionStrings": { "DefaultConnection": "Server=LAPTOP-JI9JVS93 SQLEXPRESS02;Database=EmployeesDb;Trusted_connection=true;TrustServerCertific ate=true;" }  Defines the SQL Server connection string.  Trusted_connection=true; → Uses Windows Authentication.  TrustServerCertificate=true; → Accepts self-signed certificates.
  • 14.
    1.13 Database inSQL Server Management Studio (SSMS) 💾  The Employees table is created in EmployeesDb.  The table contains actual employee records.  You can run SQL queries like:  SELECT * FROM Employees;
  • 15.
    1.14 Swagger UI(API Testing) 📜  Swagger provides an interactive API documentation.  ️ 🛠️ GET /api/employees → Lists all employees.  🛠️ POST /api/employees → Adds a new employee.  Each API call in Swagger shows:  Request Body  Response Status (200 OK, 400 Bad Request, etc.)  Response Body (JSON data)
  • 16.
    1.15 Summary Component Purpose Program.csConfigures API, middleware, and DB connection. appsettings.json Stores DB connection and logging settings. Models (Employee, DTOs) Defines data structures. Controllers (EmployeesController) Handles HTTP requests. Data (ApplicationDbContext) Connects to the database using EF Core. Migrations Defines database schema changes. Swagger UI Provides API documentation and testing.
  • 17.

Editor's Notes

  • #8 How It Works: ✅ [ApiController] → Enables API-related features like validation. ✅ [Route("api/hello")] → Defines the endpoint. ✅ [HttpGet] → Specifies that this method handles GET requests.
  • #9 This model is used to send and receive data in the API.
  • #10 Defines how the Employees table should be created in the database.
  • #11 DTOs (Data Transfer Objects) transfer data between client and server while hiding sensitive database details.
  • #12 Configures API, middleware, and DB connection.
  • #15 Swagger is an API documentation tool that helps developers visualize, test, and interact with API endpoints.