ASP.Net Core - Web API.pdf - Asp.Net Core Web API By...

This preview shows page 1 - 13 out of 56 pages.

Asp.Net Core Web API By: Mahboob Ali
Objectives Intro to Web API Web API Controllers Web API Routing Web API Model Binding Web API Content Negotiation Building a Web API Service Hosting a Web API Service Consuming a Web API Service from .NET Consuming a Web API Service from JavaScript
Motivation Not all controllers are used to send HTML documents to clients. There are also API controllers , which are used to provide access to an application’s data. This feature used to be through a separate framework called Web API framework. But in ASP.NET Core this feature is now integrated into ASP.NET Core MVC.
What is a Web API? What is an API? An application programming interface (API) is a set of subroutine definitions, protocols, and tools for building software and applications. (Wikipedia). API in other words is some kind of an interface includes different methods (functions), allows the programmers to access specific features or data of an application, operating system or other services
API Controllers API controllers are like regular controllers, except that the responses produced by their action methods are data objects that are sent to the client without HTML markup.
Why are they useful? Not all clients are browsers. Neither all clients present data to users. An API controller provides an open support to the application so that other clients can be connected or clients developed by the third part also can get support with the application. API controllers allow clients to access the data in an application without receiving the HTML markup.
How are they used? API controller are used just like regular HTML controllers
Prepare the Example Project Create a new project, I named the project “ APIControllers ”. Select an ASP.NET Web Application (.NET Core) From the template choose the Empty template .
Creating the Model and Repository Create the models folder and then create your first model class called Reservation.ca namespace APIControllers.Models { public class Reservation { public int ReservationId { get; set; } public string ClientName { get; set; } public string Location { get; set; } } }
Creating an Interface: IRepository Create your repository class called IRepository.cs (An Interface) in the Models folder using System.Collections.Generic; namespace APIControllers.Models { public interface IRepository { IEnumerable<Reservation> Reservations { get; } Reservation this[int id] { get; } Reservation AddReservation(Reservation reservation); Reservation UpdateReservation(Reservation reservation); void DeleteReservation(int id); } }
Memory Repository (Storage) Adding a nonpersistent implementation of the IRepository interface in Models folder, add a MemoryRepository.cs using System.Collections.Generic; namespace APIControllers.Models {public class MemoryRepository : IRepository { private Dictionary<int, Reservation> items; public MemoryRepository() {items = new Dictionary<int, Reservation>(); new List<Reservation> { new Reservation{ ClientName = "Alice", Location = "Board Room" }, new Reservation{ ClientName = "Bob", Location = "Lecture Hall" }, new Reservation{ClientName = "Joe", Location = "Meeting Room 1" } }.ForEach(r => AddReservation(r)); }
public Reservation this[int id] => items.ContainsKey(id) ? items[id] : null;

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture