From 8ffb983455a3807c744caaffbaac1d6f98859a10 Mon Sep 17 00:00:00 2001 From: liu <3020789065> Date: Sun, 2 Jun 2024 20:58:45 +0800 Subject: [PATCH] zy --- .../2024.05.27-CRUD.md" | 37 +++++++++++++ .../2024.05.28-\350\277\207\346\273\244.md" | 53 ++++++++++++++++++ ...13\346\225\260\346\215\256\345\272\223.md" | 21 ++++++++ .../2024.05.31-\346\216\245\345\217\243.md" | 54 +++++++++++++++++++ 4 files changed, 165 insertions(+) create mode 100644 "\345\210\230\344\270\234\345\275\252/2024.05.27-CRUD.md" create mode 100644 "\345\210\230\344\270\234\345\275\252/2024.05.28-\350\277\207\346\273\244.md" create mode 100644 "\345\210\230\344\270\234\345\275\252/2024.05.30-\345\206\205\345\255\230\345\236\213\346\225\260\346\215\256\345\272\223.md" create mode 100644 "\345\210\230\344\270\234\345\275\252/2024.05.31-\346\216\245\345\217\243.md" diff --git "a/\345\210\230\344\270\234\345\275\252/2024.05.27-CRUD.md" "b/\345\210\230\344\270\234\345\275\252/2024.05.27-CRUD.md" new file mode 100644 index 0000000..fe46d66 --- /dev/null +++ "b/\345\210\230\344\270\234\345\275\252/2024.05.27-CRUD.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/\345\210\230\344\270\234\345\275\252/2024.05.28-\350\277\207\346\273\244.md" "b/\345\210\230\344\270\234\345\275\252/2024.05.28-\350\277\207\346\273\244.md" new file mode 100644 index 0000000..640304a --- /dev/null +++ "b/\345\210\230\344\270\234\345\275\252/2024.05.28-\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/\345\210\230\344\270\234\345\275\252/2024.05.30-\345\206\205\345\255\230\345\236\213\346\225\260\346\215\256\345\272\223.md" "b/\345\210\230\344\270\234\345\275\252/2024.05.30-\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..7b95aaa --- /dev/null +++ "b/\345\210\230\344\270\234\345\275\252/2024.05.30-\345\206\205\345\255\230\345\236\213\346\225\260\346\215\256\345\272\223.md" @@ -0,0 +1,21 @@ +## 内存型数据库 +- 创建一个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 + }) + } +``` + +### 各类文件夹的代指 +- `Db`指的是内存型数据库,自己创建的数据库 +- `Domain`指的是实体类对象,用来放字段 +- `interfaces`指的是接口类对象(`I`实例名`Repository`结尾) +- `Services`是接口的实现类(实例名`Repository`结尾) \ No newline at end of file diff --git "a/\345\210\230\344\270\234\345\275\252/2024.05.31-\346\216\245\345\217\243.md" "b/\345\210\230\344\270\234\345\275\252/2024.05.31-\346\216\245\345\217\243.md" new file mode 100644 index 0000000..eaa99df --- /dev/null +++ "b/\345\210\230\344\270\234\345\275\252/2024.05.31-\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