From 4b84330a7e9c597d5651d39413194be19e1d0b05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=A0=E7=BF=8A=E5=86=9B?= <2182174962@qq.com> Date: Sun, 2 Jun 2024 11:25:37 +0000 Subject: [PATCH] =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 占翊军 <2182174962@qq.com> --- .../20240527-\350\267\257\347\224\261.md" | 62 +++++++++++++++++++ ...8-\350\277\207\346\273\244\345\231\250.md" | 48 ++++++++++++++ ...04\346\272\220\346\223\215\344\275\234.md" | 25 ++++++++ ...23\345\202\250\347\263\273\347\273\237.md" | 24 +++++++ 4 files changed, 159 insertions(+) create mode 100644 "\345\215\240\347\277\212\345\206\233/20240527-\350\267\257\347\224\261.md" create mode 100644 "\345\215\240\347\277\212\345\206\233/20240528-\350\277\207\346\273\244\345\231\250.md" create mode 100644 "\345\215\240\347\277\212\345\206\233/20240530-\350\265\204\346\272\220\346\223\215\344\275\234.md" create mode 100644 "\345\215\240\347\277\212\345\206\233/20240531-\344\273\223\345\202\250\347\263\273\347\273\237.md" diff --git "a/\345\215\240\347\277\212\345\206\233/20240527-\350\267\257\347\224\261.md" "b/\345\215\240\347\277\212\345\206\233/20240527-\350\267\257\347\224\261.md" new file mode 100644 index 0000000..db617f4 --- /dev/null +++ "b/\345\215\240\347\277\212\345\206\233/20240527-\350\267\257\347\224\261.md" @@ -0,0 +1,62 @@ +### 路由: +``` +[ApiController].[Route("/api/..")] +``` + +### 关于匹配到函数的处理: + 1. 入参:函数的参数,模型绑定 +2. 出参:返回的响应,1.状态码 2.带对象的状态码 3.重定向 4.内容 + + +``` +Dto:BlogDto.cs + +namespace Admin2024.Api; + +public class BlogDto{ + public string Title {get;set;}=null!; + public string Author {get;set;}=null!; + public string? Flag {get;set;} + +} +``` + + +``` +BlogsController.cs: +using Microsoft.AspNetCore.Mvc; + +namespace Admin2024.Api; + +[ApiController] +[Route("/api/[controller]")] + +public class BlogsController:ControllerBase{ +//创建 + public IActionResult Index(){ + return Ok("你好,世界"); + } + + [Route("{id}")] + public IActionResult Details(int id){ + return Ok(id); + } +//新增 + [HttpPost] + public ActionResult Post(BlogDto blogDto){ + return blogDto; + } +//编辑 + [HttpPut("{id}")] + public ActionResult PUT(int id,BlogDto blogDto){ + return new {id,blogDto}; + } +//删除 + [HttpDelete] + public IActionResult Delete(int id){ + return Ok(id); + } + + +} +``` \ No newline at end of file diff --git "a/\345\215\240\347\277\212\345\206\233/20240528-\350\277\207\346\273\244\345\231\250.md" "b/\345\215\240\347\277\212\345\206\233/20240528-\350\277\207\346\273\244\345\231\250.md" new file mode 100644 index 0000000..bf17ec8 --- /dev/null +++ "b/\345\215\240\347\277\212\345\206\233/20240528-\350\277\207\346\273\244\345\231\250.md" @@ -0,0 +1,48 @@ +## 过滤器 + ### Filter +``` + +在默认的WebApi中,框架提供了三种Filter,他们的功能和运行条件如下表所示: + +Filter 类型 实现的接口 描述 +Authorization IAuthorizationFilter 最先运行的Filter,被用作请求权限校验 +Action IActionFilter 在Action运行的前、后运行 +Exception IExceptionFilter 当异常发生的时候运行 +``` +``` +建Filter ApiResultFilter.cs: + +namespace Admin2024.Api; + +public class ApiResultFilter:IAsyncResultFilter{ + public Task OnResultExecutionAsync(ResultExecutingContent content,ResultExectionDeletegate next){ + //判断返回结果是否是内容,如果是,我们就给content.Result赋一个新的实例对象 + if(content.Result is objectResult){ + var objectResult=(objectResult)content.Result; + content.Result=new objectResult(new ApiResult{ + Code=1000, + Msg="请求成功", + Data=objectResult.Value + }) + }else{ + content.Result=new objectResult(new ApiResult{ + Code=1001 + }) + await next(); + } + } +} +``` + +``` +Dto ApiResult.cs + +namespace Admin2024.Api; + +public class ApiResult{ + public int Code{get;set;} + public string? Msg{get;set;} + public object? Data{get;set;} + +} +``` \ No newline at end of file diff --git "a/\345\215\240\347\277\212\345\206\233/20240530-\350\265\204\346\272\220\346\223\215\344\275\234.md" "b/\345\215\240\347\277\212\345\206\233/20240530-\350\265\204\346\272\220\346\223\215\344\275\234.md" new file mode 100644 index 0000000..11be29e --- /dev/null +++ "b/\345\215\240\347\277\212\345\206\233/20240530-\350\265\204\346\272\220\346\223\215\344\275\234.md" @@ -0,0 +1,25 @@ +#### 项目创建简易步骤 +一.主体结构: + ①:Resultfull风格的webAApi + ②:Asp.net Core 8.0 + ③:采用传统控制器模式 + +二. 模型设计(图书管理系统) + 作者:(Authors) + 姓名:AuthorName + 性别:Gender + 出生年月:Birthday + + 图书: + 书名:BookName + 出版社:Publister + 作者:AuthorId + 价格:Price + +三.内存型数据库(自定义的一个集合类) + +四.仓储系统(准备用来接入数据库的一套接口) + ①:通用型仓储接口 + ②:个性化的仓储系统(作者的增删改查,图书的增删改查) + +五.利用第一步的主体结构,具体实现Resultfull风格的关于作者,图书的CRUD diff --git "a/\345\215\240\347\277\212\345\206\233/20240531-\344\273\223\345\202\250\347\263\273\347\273\237.md" "b/\345\215\240\347\277\212\345\206\233/20240531-\344\273\223\345\202\250\347\263\273\347\273\237.md" new file mode 100644 index 0000000..8efdb08 --- /dev/null +++ "b/\345\215\240\347\277\212\345\206\233/20240531-\344\273\223\345\202\250\347\263\273\347\273\237.md" @@ -0,0 +1,24 @@ +### 仓储系统 +``` +public interface IAuthorRepository{ + 1.//通过id获取指定作者的方法 + 2.//获取所有作者的方法 + 3.//函数三要数:函数名称,函数参数,函数返回值 +} +``` + +``` +建Services(实现类)-AuthorRespository.cs + +using BookStore.Api.Domain; +using BookStore.Api.Db; +namespace class AuthorRepository;IAthorRepository{ + public ICollectionGetAllAuthors(){ + //这个实现应该从持久化的数据源中获取数据库文件名称,各种异构数据,从各种api中获取的数据 + } +} + +public Authors GetAuthorById(Guid id){ + +} +``` \ No newline at end of file -- Gitee