From 9d250cc1c419181bb8ee6f2cd82a338cb4004edf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=92=B2=E6=98=8C=E6=9C=88?= <2821684800@qq.com> Date: Sun, 2 Jun 2024 13:01:06 +0000 Subject: [PATCH] =?UTF-8?q?=E7=AC=94=E8=AE=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 蒲昌月 <2821684800@qq.com> --- ...45\215\225\345\256\214\346\210\220CRUD.md" | 37 +++++++++++++ .../20240528-\350\277\207\346\273\244.md" | 53 ++++++++++++++++++ ...13\346\225\260\346\215\256\345\272\223.md" | 26 +++++++++ .../20240531-\346\216\245\345\217\243.md" | 54 +++++++++++++++++++ 4 files changed, 170 insertions(+) create mode 100644 "\350\222\262\346\230\214\346\234\210/20240527-\346\216\247\345\210\266\345\231\250\347\256\200\345\215\225\345\256\214\346\210\220CRUD.md" create mode 100644 "\350\222\262\346\230\214\346\234\210/20240528-\350\277\207\346\273\244.md" create mode 100644 "\350\222\262\346\230\214\346\234\210/20240530-\345\206\205\345\255\230\345\236\213\346\225\260\346\215\256\345\272\223.md" create mode 100644 "\350\222\262\346\230\214\346\234\210/20240531-\346\216\245\345\217\243.md" diff --git "a/\350\222\262\346\230\214\346\234\210/20240527-\346\216\247\345\210\266\345\231\250\347\256\200\345\215\225\345\256\214\346\210\220CRUD.md" "b/\350\222\262\346\230\214\346\234\210/20240527-\346\216\247\345\210\266\345\231\250\347\256\200\345\215\225\345\256\214\346\210\220CRUD.md" new file mode 100644 index 0000000..fe46d66 --- /dev/null +++ "b/\350\222\262\346\230\214\346\234\210/20240527-\346\216\247\345\210\266\345\231\250\347\256\200\345\215\225\345\256\214\346\210\220CRUD.md" @@ -0,0 +1,37 @@ +## 控制器简单完成CRUD +```csharp +using Microsoft.AspNetCore.Mvc; + +namespace Admin.api; +//实现自动查找路由(可无脑加) +[ApiController] +//定义路由,[controller]代指`/blog` +[Route("[controller]")] +public class BlogController : ControllerBase +{ + //获取到所有或者指定id的数据 + [HttpGet("{id?}")] + public IActionResult Index(int id) + { + return Ok(new {Data=id}); + } + //保存数据,BlogDto为定义的实体类对象,注意返回类型为ActionResult<实体类对象> + [HttpPost] + public ActionResult Edit(BlogDto blogDto) + { + return blogDto; + } + //删除数据 + [HttpDelete] + public IActionResult Delete() + { + return Ok(new {Code=3000,Msg="删除成功"}); + } + //修改数据,BlogDto为定义的实体类对象,注意返回类型为ActionResult<实体类对象> + [HttpPut("{id}")] + public ActionResult Put(int id,BlogDto blogDto) + { + return Ok(new {Code=3000,Msg="修改成功",id=id,Data=blogDto}); + } +} +``` \ No newline at end of file diff --git "a/\350\222\262\346\230\214\346\234\210/20240528-\350\277\207\346\273\244.md" "b/\350\222\262\346\230\214\346\234\210/20240528-\350\277\207\346\273\244.md" new file mode 100644 index 0000000..640304a --- /dev/null +++ "b/\350\222\262\346\230\214\346\234\210/20240528-\350\277\207\346\273\244.md" @@ -0,0 +1,53 @@ +### 过滤 +1. 概念: + - 控制器过滤器:在ASP.NET Core中,控制器过滤器是用于拦截请求和响应的组件,可以用于执行诸如验证、授权、日志记录和格式化响应等任务。 + - 统一返回格式:你可以创建一个自定义的控制器过滤器,用于在返回数据之前对其进行格式化,确保所有响应都遵循{code, msg, data}的结构。 +2. 实现 + - 创建的Filter控制器中 + ```csharp + using Microsoft.AspNetCore.Mvc; + using Microsoft.AspNetCore.Mvc.Filters; + using System; + + public class CustomResponseFilterAttribute : ActionFilterAttribute + { + public override void OnActionExecuted(ActionExecutedContext context) + { + var result = context.Result as ObjectResult; + if (result == null) + { + return; + } + + var data = result.Value; + var response = new + { + code = result.StatusCode ?? 200, // 默认成功状态码 + msg = "success", // 默认消息 + data = data + }; + + result.Value = response; + } + } + ``` + - Startup + ```csharp + public void ConfigureServices(IServiceCollection services) + { + services.AddControllers(options => + { + options.Filters.Add(typeof(CustomResponseFilterAttribute)); + }); + } + ``` + - 控制器中使用 + ```csharp + [CustomResponseFilter] + public IActionResult GetData() + { + + var data = ...; // 获取数据 + return new ObjectResult(data); + } + ``` \ No newline at end of file diff --git "a/\350\222\262\346\230\214\346\234\210/20240530-\345\206\205\345\255\230\345\236\213\346\225\260\346\215\256\345\272\223.md" "b/\350\222\262\346\230\214\346\234\210/20240530-\345\206\205\345\255\230\345\236\213\346\225\260\346\215\256\345\272\223.md" new file mode 100644 index 0000000..3dc31e8 --- /dev/null +++ "b/\350\222\262\346\230\214\346\234\210/20240530-\345\206\205\345\255\230\345\236\213\346\225\260\346\215\256\345\272\223.md" @@ -0,0 +1,26 @@ +## 内存型数据库 +- 创建一个Db对象 +```csharp + public static BookDtoreDb Instance{get;set;}=new BookStoreDb(); + public Icollection<实例对象1> 名1 {get;set;}=new List<实例对象1>(); + public Icollection<实例对象2> 名2 {get;set;}=new List<实例对象2>(); + //初始化数据 + public BookStoreDb() + { + 名1.Add(new 实例名{ + 字段1=值1, + 字段2=值2 + }) + } +``` + +### 集合的操作 +> 较多使用:ICollection、IList、IComparer + +![alt text](./imgs/i..Repository.png) + +### 各类文件夹的代指 +- `Db`指的是内存型数据库,自己创建的数据库 +- `Domain`指的是实体类对象,用来放字段 +- `interfaces`指的是接口类对象(`I`实例名`Repository`结尾) +- `Services`是接口的实现类(实例名`Repository`结尾) \ No newline at end of file diff --git "a/\350\222\262\346\230\214\346\234\210/20240531-\346\216\245\345\217\243.md" "b/\350\222\262\346\230\214\346\234\210/20240531-\346\216\245\345\217\243.md" new file mode 100644 index 0000000..eaa99df --- /dev/null +++ "b/\350\222\262\346\230\214\346\234\210/20240531-\346\216\245\345\217\243.md" @@ -0,0 +1,54 @@ +## 接口 +- I实例名Repository + ```csharp + //通过Id获取所有作者的实现 + 实例对象? GetAuthorById(Guid guid); + + //获取所有作者的方法 + //函数三要素(函数名,形参,返回值) + ICollection<实例对象> GetAllAuthors(); + + ``` +- 实例名Repository + ```csharp + public class 实例名Repository:I实例名Repository + { + public ICollection<实例名> GetAllAuthors() + { + //从持久化数据库中获取 + return 名Db.Instance.实例名.ToList(); + //return [.. 名Db.Instance.实例名]; + } + public 实例名? GetAuthorById(Guid guid) + { + return 名Db.Instance.实例名.SingleOrDefault(item=>item.Id==guid); + } + } + ``` +- 控制器中 + ```csharp + private readonly I实例名Repository _实例名Repository; + public 实例名Controller(I实例名Repository 实例名Repository) + { + _实例名Repository=实例名Repository; + } + //获取get的函数中 + [HttpGet("{id?}")] + public IActionResult Get(int id) + { + if(id.ToString==""){ + return Ok(_实例名Repository.GetAllAuthors()); + }else{ + return Ok(_实例名Repository.GetAuthorById(id)); + } + } + ``` +- Startup + ```csharp + public void ConfigureServices(IServiceCollection services) + { + services.AddControllers(); + //新加 + services.AddScoped(); + } + ``` \ No newline at end of file -- Gitee