使用Flask实现基本HTTP谓词操作
1 目标
使用DotNet实现基本HTTP谓词操作。
HTTP 谓词 | 功能 | 用途 | 示例 |
---|---|---|---|
GET | 获取资源 | 从服务器检索数据,不会对服务器上的数据产生任何影响 | 获取所有项目:GET /items 获取指定ID的项目:GET /items/1 |
POST | 创建资源 | 向服务器发送数据,并在服务器上创建新的资源 | 添加新项目:POST /items 请求体:{ “name”: “New Item”, “description”: “New Description” } |
PUT | 更新资源(替换整个资源) | 替换服务器上的现有资源 | 更新项目:PUT /items/1 请求体:{ “name”: “Updated Item”, “description”: “Updated Description” } |
PATCH | 更新资源(部分更新资源) | 更新服务器上的现有资源的部分内容 | 部分更新项目:PATCH /items/1 请求体:{ “description”: “Partially Updated Description” } |
DELETE | 删除资源 | 从服务器上删除指定资源 | 删除项目:DELETE /items/1 |
2 代码
2.1 Item.cs
在工程目录下新建一个Models目录用于存放模型类。然后在此目录下新建Item.cs文件
namespace WebApplication1.Models
{
// 定义一个 Item 类,用于表示项目数据
public class Item
{
// 项目 ID,唯一标识
public int Id { get; set; }
// 项目名称,required 属性表示此字段是必需的
public required string Name { get; set; }
// 项目描述,允许为空(可选字段)
public string? Description { get; set; }
}
}
2.2 ItemsController.cs
在Controllers目录下新建一个ItemsController.cs文件。
using Microsoft.AspNetCore.Mvc;
using WebApplication1.Models;
using System.Collections.Generic;
using System.Linq;
namespace WebApplication1.Controllers
{
[Route("api/[controller]")] // 路由特性,设置控制器的基本路由
[ApiController] // 指定这是一个 API 控制器
public class ItemsController : ControllerBase
{
// 初始化一些示例数据
private static List<Item> items = new List<Item>
{
new Item { Id = 1, Name = "Item 1", Description = "Description 1" },
new Item { Id = 2, Name = "Item 2", Description = "Description 2" }
};
// GET: api/items
[HttpGet]
public ActionResult<IEnumerable<Item>> GetItems()
{
// 返回所有项目
return items;
}
// GET: api/items/1
[HttpGet("{id}")]
public ActionResult<Item> GetItem(int id)
{
// 根据 ID 获取项目
var item = items.FirstOrDefault(i => i.Id == id);
if (item == null)
{
// 如果找不到项目,返回 404 错误
return NotFound("Item not found");
}
// 返回找到的项目
return item;
}
// POST: api/items
[HttpPost]
public ActionResult<Item> PostItem(Item newItem)
{
// 为新项目设置 ID
newItem.Id = items.Max(i => i.Id) + 1;
// 添加新项目到列表
items.Add(newItem);
// 返回创建的项目,并设置 201 状态码
return CreatedAtAction(nameof(GetItem), new { id = newItem.Id }, newItem);
}
// PUT: api/items/1
[HttpPut("{id}")]
public ActionResult<Item> PutItem(int id, Item updatedItem)
{
// 查找要更新的项目索引
var itemIndex = items.FindIndex(i => i.Id == id);
if (itemIndex == -1)
{
// 如果找不到项目,返回 404 错误
return NotFound("Item not found");
}
// 更新项目的属性
items[itemIndex].Name = updatedItem.Name;
items[itemIndex].Description = updatedItem.Description;
// 返回更新后的项目
return items[itemIndex];
}
// PATCH: api/items/1
[HttpPatch("{id}")]
public ActionResult<Item> PatchItem(int id, [FromBody] Item updatedFields)
{
// 根据 ID 查找项目
var item = items.FirstOrDefault(i => i.Id == id);
if (item == null)
{
// 如果找不到项目,返回 404 错误
return NotFound("Item not found");
}
// 更新项目的部分字段
if (updatedFields.Name != null)
{
item.Name = updatedFields.Name;
}
if (updatedFields.Description != null)
{
item.Description = updatedFields.Description;
}
// 返回更新后的项目
return item;
}
// DELETE: api/items/1
[HttpDelete("{id}")]
public ActionResult<Item> DeleteItem(int id)
{
// 查找要删除的项目索引
var itemIndex = items.FindIndex(i => i.Id == id);
if (itemIndex == -1)
{
// 如果找不到项目,返回 404 错误
return NotFound("Item not found");
}
// 删除项目并返回已删除的项目
var deletedItem = items[itemIndex];
items.RemoveAt(itemIndex);
return deletedItem;
}
}
}