0% found this document useful (0 votes)
20 views46 pages

Web API On

This document provides an overview of using NoSQL on ASP.NET. It discusses ASP.NET frameworks like Web Forms, MVC, and Web API. It then explains what a Web API is and describes common REST methods. Next, it outlines the client-server architecture with a request and response. It also defines JSON and how it is used to represent REST responses. Finally, it provides steps to set up a JSON server using Node.js.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
20 views46 pages

Web API On

This document provides an overview of using NoSQL on ASP.NET. It discusses ASP.NET frameworks like Web Forms, MVC, and Web API. It then explains what a Web API is and describes common REST methods. Next, it outlines the client-server architecture with a request and response. It also defines JSON and how it is used to represent REST responses. Finally, it provides steps to set up a JSON server using Node.js.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 46

NoSQL on ASP.

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.

• A Web API is an application programming interface for the Web.


• A Browser API can extend the functionality of a web browser.
• A Server API can extend the functionality of a web server.

• 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-SERVER ARCHITECTURE Request Response

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

2 Open command prompt or cmd

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

Note : Back up json file before test

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

From the File menu, select New > Project.

Select the ASP.NET Core Web API template and


click Next.

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

Adds the database context to the DI container.


Specifies that the database context will use an in-memory
database.

issarapong.khu@kmutt.ac.th 34
Scaffold a controller

1 2 Select API Controller with action, using Entity Framework

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]

The routing format is set in the Configure method in Startup.cs


file.
or press F5

https://localhost:xxxxport/HelloWorld/Welcome

For example, localhost:xxxxport/HelloWorld maps to the


HelloWorldController class.

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:

Building REST API with SQL Server using JSON functions


https://youtu.be/0m6GXF3-5WI

issarapong.khu@kmutt.ac.th 46

You might also like