当前位置:网站首页>Using asp Net core creating web API series
Using asp Net core creating web API series
2022-06-30 15:40:00 【zhoubangbang1】
Use ASP.NET Core establish Web API
This time mainly introduces the use of ASP.NET Core structure Web API Basic knowledge of .
In this tutorial , You will understand :
- establish Web API project .
- Add model classes and database context .
- Use CRUD Method to build the controller .
- Configure the routing 、URL Path and return value .
- Use Postman call Web API.
summary This tutorial will create the following API:
API | explain | Request body | Response Content |
GET /api/TodoItems | Get all the to-do items | nothing | An array of to-do items |
GET /api/TodoItems/{id} | Press ID To obtain a | nothing | To do list |
POST /api/TodoItems | Add a new item | To do list | To do list |
PUT /api/TodoItems/{id} | Update existing items | To do list | nothing |
DELETE /api/TodoItems/{id} | Delete the item | nothing | nothing |
establish Web project
from “ file ” From the menu “ newly build ”>“ project ” .
choice “ASP.NET Core Web Applications ” Templates , Click again “ next step ” .
Name the project TodoApi, And then click “ establish ” .
stay “ Create a new ASP.NET Core Web Applications ” In the dialog box , Confirm selection “.NET Core” and “ASP.NET Core 3.1” . choice “API” Templates , And then click “ establish ” .

Add model class
Model Is a group of classes that represent application management data . The model for this application is a single TodoItem class .
- stay “ Solution explorer ” in , Right click on the item . choice “ add to ” > “ New folder ” . Name the folder “Models” .
- Right click “Models” Folder , And then choose “ add to ” > “ class ” . Name the class TodoItem, And then choose “ add to ” .
- Replace the template code with the following code :
Build controller
- Right click Controllers Folder .
- choice “ add to ”>“ New build item ” .
- choice “ Its operation uses Entity Framework API controller ”, And then choose “ add to ”
Check PostTodoItem create Method
Replace PostTodoItem The return statement in , To use the nameof Operator :
[Route("api/posttodoitem")]
[ApiController]
public class PostTodoItemController : ControllerBase
{
public static List<TodoItem> todoItems = new List<TodoItem>() {
new TodoItem{Id=1,IsComplete=true,Name=" Zhang Yi Xing " },
new TodoItem{Id=2,IsComplete=true,Name=" Zheng Kai " },
new TodoItem{Id=3,IsComplete=true,Name=" Angela Baby " },
new TodoItem{Id=4,IsComplete=true,Name=" Chen He " },
new TodoItem{Id=5,IsComplete=true,Name=" Li Chen " },
};
[HttpPost]
public async Task<ActionResult<TodoItem>> PostTodoItem(TodoItem todoItem)
{
todoItems.Add(todoItem);
return CreatedAtAction(nameof(GetTodoItem), new { id = todoItem.Id }, todoItem);
// return CreatedAtAction("GetTodoItem", new { id = todoItem.Id }, todoItem);
}
[HttpGet]
public async Task<ActionResult<List<TodoItem>>> TodoItems()
{
return await Task.Run(() => todoItems);
// return CreatedAtAction("GetTodoItem", new { id = todoItem.Id }, todoItem);
}
[HttpGet("{id}")]
public async Task<ActionResult<TodoItem>>GetTodoItem(int id)
{
var todoItem = await Task.Run(() => todoItems.Where(x => x.Id == id)); ;
if (todoItem == null)
{
return NotFound();
}
return todoItem.FirstOrDefault();
}
}install Postman
This tutorial USES Postman test Web API.
- install Postman
- start-up Web application \( At testing time , It can be found in the project folder , function cmd
- Input :dotnet watch run )
For example, the project location is :

open Cmd

- start-up Postman.
- Ban SSL Certificate validation
- stay “ file ”>“ Set up ”(“ routine ” tab ) in , Ban “SSL Certificate validation ”
adopt Postman test PostTodoItem
- Create a new request .
- take HTTP Method set to
POST. - choice “ Text ” tab .
- choice “ original ” Radio button .
- Set type to JSON (application/json)
- In the body of the request , Enter the to-do list JSON:
{
Id:5
"name":"walk dog",
"isComplete":true
}
choice Send.

Test location header URI
- stay Headers Select... In the pane Response tab .
- Copy Location Header value :

- Set the method to “GET”.
- Paste URI( for example ,http://localhost:52655/api/posttodoitem/6).
- choice Send.

边栏推荐
- Introduction to using 51 single chip microcomputer to control steering gear
- Google Play 索引表
- Joint examination for management -- sample composition
- topic: Privacy, Deception and Device Abuse
- Rte2021 review HDR technology product practice and exploration
- Warning: [antd: Menu] `children` will be removed in next major version. Please use `items` instead.
- 【时序数据库InfluxDB】Windows环境下配置InfluxDB+数据可视化,以及使用 C#进行简单操作的代码实例
- 1015 reversible primes (20 points)
- Some reference routines for cache update
- Pycharm----xx. So cannot open shared object file problem solving
猜你喜欢

What would you choose between architecture optimization and business iteration?

NPM install --global --save --save dev differences
![[matlab] 2D drawing summary](/img/de/6bb5385f440a2997dbf9cbb9a756eb.jpg)
[matlab] 2D drawing summary

Npumcm selection question 3 and acmc2020a

Teach you a learning method to quickly master knowledge

Anyrtc implements application scenarios based on webrtc

How should we understand the variability of architecture design?

数数据可视化实战案例(timeline轮播图,streamlit 控件年份 metabase可视化使用教程)2.0

Industry analysis | the future of real-time audio and video

Map reduce case super detailed explanation
随机推荐
[matlab] 3D drawing summary
Is Domain Driven Design (DDD) reliable?
G - building a space station
Infrastructure is code. What are you talking about?
Basic literacy - four common software architectures
What would you choose between architecture optimization and business iteration?
1030 travel plan (30 points)
1077 kuchiguse (20 points)
Advanced functions of ES6 operation array map (), filter (), reduce()
Summary of system stability construction practice
H - Arctic network (minimum spanning tree)
J - Borg maze (minimum spanning tree +bfs)
Help you accumulate audio and video knowledge, Agora developer's roaming guide officially set sail
1019 general palindromic number (20 points)
1082 read number in Chinese (25 points)
深入理解.Net中的线程同步之构造模式(二)内核模式1.内核模式构造物Event事件
Explain service idempotency design in detail
E - highways (minimum spanning tree)
Talk about why I started technical writing
001 data type [basic]