From 82724a284c9d416ed3f326ed095569a010601a3d Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 2 Jun 2024 22:15:42 +0800 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 --- .../2024.5.27\350\267\257\347\224\261.md" | 53 ++++++++++++ ...28\350\277\207\346\273\244\345\231\250.md" | 44 ++++++++++ ...17\345\236\213\351\241\271\347\233\256.md" | 85 +++++++++++++++++++ ...06\345\220\210\346\216\245\345\217\243.md" | 28 ++++++ 4 files changed, 210 insertions(+) create mode 100644 "\351\253\230\344\277\212\346\235\260/2024.5.27\350\267\257\347\224\261.md" create mode 100644 "\351\253\230\344\277\212\346\235\260/2024.5.28\350\277\207\346\273\244\345\231\250.md" create mode 100644 "\351\253\230\344\277\212\346\235\260/2024.5.30\345\260\217\345\236\213\351\241\271\347\233\256.md" create mode 100644 "\351\253\230\344\277\212\346\235\260/2024.5.31\345\270\270\350\247\201\347\232\204\351\233\206\345\220\210\346\216\245\345\217\243.md" diff --git "a/\351\253\230\344\277\212\346\235\260/2024.5.27\350\267\257\347\224\261.md" "b/\351\253\230\344\277\212\346\235\260/2024.5.27\350\267\257\347\224\261.md" new file mode 100644 index 0000000..5251e34 --- /dev/null +++ "b/\351\253\230\344\277\212\346\235\260/2024.5.27\350\267\257\347\224\261.md" @@ -0,0 +1,53 @@ +### 关于匹配到函数的处理: + +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/\351\253\230\344\277\212\346\235\260/2024.5.28\350\277\207\346\273\244\345\231\250.md" "b/\351\253\230\344\277\212\346\235\260/2024.5.28\350\277\207\346\273\244\345\231\250.md" new file mode 100644 index 0000000..efc0262 --- /dev/null +++ "b/\351\253\230\344\277\212\346\235\260/2024.5.28\350\277\207\346\273\244\345\231\250.md" @@ -0,0 +1,44 @@ +## 过滤器 + +### 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/\351\253\230\344\277\212\346\235\260/2024.5.30\345\260\217\345\236\213\351\241\271\347\233\256.md" "b/\351\253\230\344\277\212\346\235\260/2024.5.30\345\260\217\345\236\213\351\241\271\347\233\256.md" new file mode 100644 index 0000000..ad430e1 --- /dev/null +++ "b/\351\253\230\344\277\212\346\235\260/2024.5.30\345\260\217\345\236\213\351\241\271\347\233\256.md" @@ -0,0 +1,85 @@ +- [小型项目](https://gitee.com/level-22-net-class/webapi-class-notes/blob/master/吴诗茵/20240530-8.小型项目.md#小型项目) + +## 小型项目 + +在Program.cs中写: + +``` +namespace BookStore.Api; + +public static class Program +{ + public static void Main(string[] args) + { + CreateWebHostBuilder(args).Build().Run(); + } + public static IHostBuilder CreateWebHostBuilder(string[] args) + { + return Host.CreateDefaultBuilder(args).ConfigureWebHostDefaults(webBuilder=> + { + webBuilder.UseStartup(); + }); + } +} +``` + +在Startup.cs中写: + +``` +namespace BookStore.Api; + +public class Startup +{ + public void Configure(IApplicationBuilder app) + { + app.UseRouting(); + app.UseEndpoints(endpoints=> + { + endpoints.MapControllers(); + }); + } + public void ConfigureServices(IServiceCollection services) + { + services.AddControllers(); + } +} +``` + +在AuthorsController.cs中写: + +``` +using BookStore.Api.Db; +using BookStore.Api.Domain; +using Microsoft.AspNetCore.Mvc; + +namespace BookStore.Api.Controller; + +[ApiController] +[Route("api/[controller]")] +public class AuthorsController : ControllerBase +{ + [HttpGet("{id?}")] + public IActionResult Get(int id) + { + var db=new BookStoreDb(); + db.Authors.Add(new Authors{AuthorName="思航航",Gender=1,Birth=Convert.ToDateTime("2000-8-8")}); + var list=db.Authors.ToList(); + return Ok(list); + } + [HttpPost("")] + public IActionResult Post() + { + return Ok(); + } + [HttpPut("{id?}")] + public IActionResult Put(int id) + { + return Ok(id); + } + [HttpDelete("{id?}")] + public IActionResult Delete(int id) + { + return Ok(id); + } +} +``` \ No newline at end of file diff --git "a/\351\253\230\344\277\212\346\235\260/2024.5.31\345\270\270\350\247\201\347\232\204\351\233\206\345\220\210\346\216\245\345\217\243.md" "b/\351\253\230\344\277\212\346\235\260/2024.5.31\345\270\270\350\247\201\347\232\204\351\233\206\345\220\210\346\216\245\345\217\243.md" new file mode 100644 index 0000000..aeea1e4 --- /dev/null +++ "b/\351\253\230\344\277\212\346\235\260/2024.5.31\345\270\270\350\247\201\347\232\204\351\233\206\345\220\210\346\216\245\345\217\243.md" @@ -0,0 +1,28 @@ +1 . Collection接口:单列集合,两个子接口 +①List接口:有序可重复 + LinkedList:基于链表实现,每个元素储存本身内存地址还储存下一个元素的地址。(增删快,查找慢) + + ArrayList:基于数组实现,每次增删都要重新创建新的数组,但数组有索引。(增删慢,查找快) + + Vector:基于数组,线程安全相关,效率低。 + +②Set接口:不可重复 + HashSet: 储存的元素无序,不可重复,底层是哈希表 + + LinkedHashSet:储存元素有序,不可重复,底层是哈希表和链表的结合 + + TreeSet:可以指定一个顺序,对象存入之后会按照指定的顺序排序。 + +2 . Map接口:双列集合 + HashMap:非线程安全,高效,支持null + +LinkedHashMap:是HashMap的一个子类,保存了记录的插入顺序 + + HashTable:线程安全,低效,不支持null + + TreeMap:能够把他保存的记录根据键排序,默认是键值的升序排序 + +3 . 注意点: +Collection和Collections的区别 + Collection 则是一个接口,Collections 是一个包装类。 +———————————————— \ No newline at end of file -- Gitee