Web API On
Web API On
NET
(updated)
Module 2 : Application
Development
issarapong.khu@kmutt.ac.th 1
Requirements
• Visual Studio
• ASP.NET
• Postman
issarapong.khu@kmutt.ac.th 2
Web Forms
Component-based
ASP.NET MVC
ASP.NET Web Forms
Web Pages • Stable and mature, supported by heaps of third-
Lightweight framework for dynamic content party controls and tools
• Event driven Web development
Web API • Postbacks
Framework for building RESTful Web services • Viewstate
• Less control over the HTML
Real-time client-server communication • Hard to test
• Rapid development
Dr.Issarapong Khuanrkrue 3
What is Web API?
• API stands for Application Programming Interface.
• Browser APIs
• All browsers have a set of built-in Web APIs to support complex operations, and to help
accessing data.
• Third Party APIs
• Third party APIs are not built into your browser. To use these APIs, you will have to download
the code from the Web.
• Examples:
• YouTube API - Allows you to display videos on a web site.
• Twitter API - Allows you to display Tweets on a web site.
• Facebook API - Allows you to display Facebook info on a web site.
issarapong.khu@kmutt.ac.th 4
• RESTful or REST stands for “Representational
State Transfer”. REST
• It is an application program interface(API) based
architecture and uses HTTP Protocol. Resource
/student
/student/1
• REST methods
GET POST
• GET − Provides a read only access to a resource. Read Create
• POST − Used to create a new resource. Get all student details Create a new student
• DELETE − Used to remove a resource.
• PUT − Used to update an existing resource or
create a new resource.
PUT DELETE
Update Delete
Update relevant student Delete relevant student
details details
Dr.Issarapong Khuanrkrue 5
Server
JSON server
Client
Browser
Dr.Issarapong Khuanrkrue 6
Ex.
{
JSON "book": [
{
"id":"01",
"language": "Java",
JSON stands for “JavaScript Object Notation”, which represent
"edition": "third",
the REST.
"author": "Herbert
Schildt"
It is a lightweight data-interchange format and "self-describing"
},
and easy to understand.
{
JSON is language independent, which uses JavaScript syntax,
"id":"07",
but the JSON format is text only. Text can be read and used as a
"language": "C++",
data format by any programming language.
"edition": "second",
"author":
"E.Balagurusamy"
}
]
}
Dr.Issarapong Khuanrkrue 7
JSON SERVER USING NODE.JS
1 Download https://nodejs.org/en/
LTS version (Recommended)
2 Install
Dr.Issarapong Khuanrkrue 8
1 Create Folder : Desktop\JsonServer
PC name
PC name
PC name
3 Command
> npm install –g json-server
Dr.Issarapong Khuanrkrue 9
4 start server : Command >json-server --watch db.json
PC name
PC name
PC name
JsonServer is ready!
Do not close
Dr.Issarapong Khuanrkrue 10
Test: go to http://localhost:3000 or 127.0.0.1:3000
Dr.Issarapong Khuanrkrue 11
{
Replaces json file "books": [
{
"id": 1,
"title": "Angels & Demons",
"price": 9.68,
"authorId": 1
},
{
"id": 2,
"title": "The Da Vinci Code",
"price": 17.97,
"authorId": 1
},
{
"id": 3,
"title": "It",
"price": 13.16,
"authorId": 2
},
{
"id": 4,
"title": "A Game of Thrones (A Song of Ice and
copy Fire, Book 1)",
"price": 10.33,
"authorId": 3
}
],
"authors": [
{
"id": 1,
"name": "Dan Brow"
},
{
"id": 2,
"name": "Stephen King"
},
{
"id": 3,
"name": "George R. R. Martin"
}
]
}
Dr.Issarapong Khuanrkrue 12
get
http://localhost:3000/books
http://localhost:3000/authors
Dr.Issarapong Khuanrkrue 13
Sort
http://localhost:3000/books?_sort=title&_order=asc
Dr.Issarapong Khuanrkrue 14
Slice
http://localhost:3000/books?_start=2&_end=4
Dr.Issarapong Khuanrkrue 15
Paginate
http://localhost:3000/books?_page=2
http://localhost:3000/books?_page=2&_limit=3
Dr.Issarapong Khuanrkrue 16
Play with REST Client on google chrome
Dr.Issarapong Khuanrkrue 17
GET
POST
PUT
DELETE
Dr.Issarapong Khuanrkrue 18
Play on postman REST Client
https://www.postman.com/product/rest-client/
Dr.Issarapong Khuanrkrue 19
GET
POST
PUT
DELETE
issarapong.khu@kmutt.ac.th 20
ASP.NET Web APIs
• Build secure REST APIs on any platform
with C#
• With ASP.NET, use the same framework
and patterns to build both web pages
and services, side-by-side in the same
project.
Dr.Issarapong Khuanrkrue 21
issarapong.khu@kmutt.ac.th 22
https://visualstudio.microsoft.com/downloads/
issarapong.khu@kmutt.ac.th 23
issarapong.khu@kmutt.ac.th 24
Create project
Dr.Issarapong Khuanrkrue 25
In the Create a new ASP.NET Core Web
Application dialog, confirm that .NET
Core and ASP.NET Core 7are selected.
issarapong.khu@kmutt.ac.th 26
Test the project
or press F5
issarapong.khu@kmutt.ac.th 27
https://localhost:<port>/WeatherForecast
issarapong.khu@kmutt.ac.th 28
Add a model class
1 2
issarapong.khu@kmutt.ac.th 29
Replace the template code
issarapong.khu@kmutt.ac.th 30
Add a database context
issarapong.khu@kmutt.ac.th 31
Microsoft.EntityFrameworkCore.InMemory
Microsoft.EntityFrameworkCore.Design
issarapong.khu@kmutt.ac.th 32
Add new class
issarapong.khu@kmutt.ac.th 33
Register the database context
Update Program.cs
add usings
issarapong.khu@kmutt.ac.th 34
Scaffold a controller
issarapong.khu@kmutt.ac.th 35
In the Add API Controller with actions, using Entity Framework
issarapong.khu@kmutt.ac.th 36
Check controller
issarapong.khu@kmutt.ac.th 37
Update the POST create method at Controller
use the nameof operator: A nameof expression produces the name of a variable, type,
or member as the string constant.
issarapong.khu@kmutt.ac.th 38
Test the project
or press F5
issarapong.khu@kmutt.ac.th 39
Disable SSL certificate verification at Postman
issarapong.khu@kmutt.ac.th 40
Test it with Postman
port
1 3
copy
2 {
"name":"walk dog",
"isComplete":true
}
issarapong.khu@kmutt.ac.th 41
Test the location header URI 1 Add https://localhost: :<port>/api/ controllername /1
port
issarapong.khu@kmutt.ac.th 42
The default URL routing logic used by MVC uses a format like this to Description
determine what code to invoke:
URL routing logic
/[Controller]/[ActionName]/[Parameters]
https://localhost:xxxxport/HelloWorld/Welcome
Dr.Issarapong Khuanrkrue 43
Test the Put method
1 3
issarapong.khu@kmutt.ac.th 44
Test the Delete method
issarapong.khu@kmutt.ac.th 45
More Information:
issarapong.khu@kmutt.ac.th 46