From 54bdcd789436d6d2617993bec6389279cfd8d3ea Mon Sep 17 00:00:00 2001 From: Your Name Date: Tue, 28 May 2024 11:28:12 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../20240524-webapi\346\226\260demo.md" | 0 .../20240527-\350\267\257\347\224\261.md" | 96 +++++++++++++++++++ ...267\257\347\224\261\345\222\214Serilog.md" | 39 ++++++++ 3 files changed, 135 insertions(+) rename "\345\224\220\345\244\251\345\250\245/20245024-webapi\346\226\260demo.md" => "\345\224\220\345\244\251\345\250\245/20240524-webapi\346\226\260demo.md" (100%) create mode 100644 "\345\224\220\345\244\251\345\250\245/20240527-\350\267\257\347\224\261.md" create mode 100644 "\345\224\220\345\244\251\345\250\245/20240528-\350\267\257\347\224\261\345\222\214Serilog.md" diff --git "a/\345\224\220\345\244\251\345\250\245/20245024-webapi\346\226\260demo.md" "b/\345\224\220\345\244\251\345\250\245/20240524-webapi\346\226\260demo.md" similarity index 100% rename from "\345\224\220\345\244\251\345\250\245/20245024-webapi\346\226\260demo.md" rename to "\345\224\220\345\244\251\345\250\245/20240524-webapi\346\226\260demo.md" diff --git "a/\345\224\220\345\244\251\345\250\245/20240527-\350\267\257\347\224\261.md" "b/\345\224\220\345\244\251\345\250\245/20240527-\350\267\257\347\224\261.md" new file mode 100644 index 0000000..ef22f84 --- /dev/null +++ "b/\345\224\220\345\244\251\345\250\245/20240527-\350\267\257\347\224\261.md" @@ -0,0 +1,96 @@ + +### BlogsController.cs +```js +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); + } + + [HttpGet("/api/[controller]/{userId}/blogs/{blogId}")] + public IActionResult abc(int userId,int blogId) + { + return Ok(new {userId,blogId,msg="789"}); + } + + [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); + } +} + + +``` +### BlogDto.cs +```js +using System.ComponentModel.DataAnnotations; +using Microsoft.AspNetCore.Mvc; + +namespace Admin2024.Api; +public class BlogDto +{ + public string Title {get;set;}=null!; + public string Author {get;set;}=null!; + public string? Flag {get;set;} +} +``` +### http +```js +@url=http://localhost:5000 + +### +GET {{url}}/api/blogs +Accept: application/json + +### +POST {{url}}/api/blogs HTTP/1.1 +Content-Type:application/json + +{ + "title":"丰富", + "author":"就", + "flag":"会尽快" +} + +### +PUT {{url}}/api/blogs/1 HTTP/1.1 +Content-Type: application/json + +{ + "title":"哈哈哈", + "author":"得到更多话费", + "flag":"法国古古怪怪" +} + +### + DELETE {{url}}/api/blogs/1 HTTP/1.1 + +### +GET {{url}}/api/users/11/blogs/77 HTTP/1.1 +``` \ No newline at end of file diff --git "a/\345\224\220\345\244\251\345\250\245/20240528-\350\267\257\347\224\261\345\222\214Serilog.md" "b/\345\224\220\345\244\251\345\250\245/20240528-\350\267\257\347\224\261\345\222\214Serilog.md" new file mode 100644 index 0000000..85e896f --- /dev/null +++ "b/\345\224\220\345\244\251\345\250\245/20240528-\350\267\257\347\224\261\345\222\214Serilog.md" @@ -0,0 +1,39 @@ +### 1.路由 +- 在控制器上写两个特性 + + [ApiController] + + [Route("/api/...")] + +### 2.关于匹配到函数的处理 +- 入参:函数的参数,模型绑定 +- 返回值:返回的响应 + + 1.状态码 + + 2.带对象的状态码 + + 3.重定向 + + 4.内容{ + Code:1000, + data:[], + msg:'请求成功' + } +### 如果不符合restfull风格的路由的话,在action上单独写路由 +- [HttpGet("/api/...")] + +### action过滤器 +```js +public Tack OnResultExecutionAsync(ResultExecutingContext content,ResultExeationDelegate next){ + //判断返回结果是否是内容 + if(context.Result is ObjectRsult) + { + var ObjectRsult=(ObjectResult)Context.Result; + context.Result=new ApiResult({ + Code=1000, + msg="请求成功", + Data=ObjectResult.vaule + }); + }else{ + context.Result=new ObjectRsult(new ApiResult{ + Code=1001 + }); + await next(); + } +} +``` \ No newline at end of file -- Gitee From 705e76bf503fca7372e269e899809d62e35bcde0 Mon Sep 17 00:00:00 2001 From: Your Name Date: Fri, 31 May 2024 11:32:01 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...71\347\233\256\350\256\276\350\256\241.md" | 38 +++++++++++++++++++ ...23\345\202\250\347\263\273\347\273\237.md" | 17 +++++++++ 2 files changed, 55 insertions(+) create mode 100644 "\345\224\220\345\244\251\345\250\245/20240530-\351\241\271\347\233\256\350\256\276\350\256\241.md" create mode 100644 "\345\224\220\345\244\251\345\250\245/20240531-\344\273\223\345\202\250\347\263\273\347\273\237.md" diff --git "a/\345\224\220\345\244\251\345\250\245/20240530-\351\241\271\347\233\256\350\256\276\350\256\241.md" "b/\345\224\220\345\244\251\345\250\245/20240530-\351\241\271\347\233\256\350\256\276\350\256\241.md" new file mode 100644 index 0000000..47058c0 --- /dev/null +++ "b/\345\224\220\345\244\251\345\250\245/20240530-\351\241\271\347\233\256\350\256\276\350\256\241.md" @@ -0,0 +1,38 @@ +## 关于项目设计 +### 一.主体结构 + - Restfull风格的webapi + - Asp.net core 8.0 + - 采用传统控制器模式 + +### 二.模型设计 +- 作者 Authors + - 姓名 AthorName + - 性别 Gender + - 出生年月 Birthday +- 图书 BookName + - 出版社 Publiser + - 作者 AuthorId + - 价格 Price + +### 三.内存型数据-自定义的一个集合类 + +### 四.创储系统-准备用来接入数据库的一套接口 +- 通用型仓储接口 +- 个性化的仓储接口 + - 作者的 增删改查 + - 图书的 增删改查 + +### 利用第一步主体结构,具体实现Restfull风格关于作者,关于图书的CRUD +- 作者 + - 获取作者列表 Get /api/authors + - 获取指定作者 Get /api/authors/88 + - 新增作者列表 post /api/authors + - 修改作者列表 put /api/authors/4545 + - 删除作者列表 deete /api/authors/78 + +- 图书 + - 获取书本列表 Get /api/authors + - 获取指定书本 Get /api/authors/88 + - 新增书本列表 post /api/authors + - 修改书本列表 put /api/authors/4545 + - 删除书本列表 deete /api/authors/78 \ No newline at end of file diff --git "a/\345\224\220\345\244\251\345\250\245/20240531-\344\273\223\345\202\250\347\263\273\347\273\237.md" "b/\345\224\220\345\244\251\345\250\245/20240531-\344\273\223\345\202\250\347\263\273\347\273\237.md" new file mode 100644 index 0000000..207f6a7 --- /dev/null +++ "b/\345\224\220\345\244\251\345\250\245/20240531-\344\273\223\345\202\250\347\263\273\347\273\237.md" @@ -0,0 +1,17 @@ +### 四.创储系统-准备用来接入数据库的一套接口 +- 通用型仓储接口 +- 个性化的仓储接口 + - 作者的 增删改查 + - 图书的 增删改查 + + +```js +namespace BookStore.Api; +public interface IAuthorRepository +{ + //通过ID获取作者的方法 + //获取所有作者的方法 + //函数三要素:函数名称,函数参数,函数返回值 + Authors GetAUTHORbYiD (gUID ID); +} +``` -- Gitee