diff --git "a/\345\217\262\347\247\200\347\272\242/20240527_\345\237\272\346\234\254\350\267\257\347\224\261.md" "b/\345\217\262\347\247\200\347\272\242/20240527_\345\237\272\346\234\254\350\267\257\347\224\261.md" new file mode 100644 index 0000000000000000000000000000000000000000..9524e7fe3cf524f419ad316e9743152f7829e13c --- /dev/null +++ "b/\345\217\262\347\247\200\347\272\242/20240527_\345\237\272\346\234\254\350\267\257\347\224\261.md" @@ -0,0 +1,41 @@ +### 路由 + +``` + +public class ProductsController : Controller +{ + [HttpGet] + [Route("api/[controller]")] + public IActionResult GetAll() + { + + } + + [HttpGet("{id}")] + public IActionResult Get(int id) + { + + } + + [HttpPost] + public IActionResult Create([FromBody] Product product) + { + + } +} + +``` + +在这个例子中,ProductsController 的基础路由是 api/products。这是通过将 [Route("api/[controller]")] 属性应用于控制器类来实现的。随后,针对控制器中的不同操作定义了更具体的路由。 + +###### 自定义路由 + +在 Startup.Configure 方法中,可以通过 app.UseEndpoints 扩展方法来添加自定义路由。 + +app.UseEndpoints(endpoints => +{ +endpoints.MapControllerRoute( +name: "custom", +pattern: "{action=Index}/{id?}" +); +}); diff --git "a/\345\217\262\347\247\200\347\272\242/20240528_ \350\277\207\346\273\244\345\231\250.md" "b/\345\217\262\347\247\200\347\272\242/20240528_ \350\277\207\346\273\244\345\231\250.md" new file mode 100644 index 0000000000000000000000000000000000000000..7e8d3e53988f26eb07738f7fe5bd6f28a63a8d83 --- /dev/null +++ "b/\345\217\262\347\247\200\347\272\242/20240528_ \350\277\207\346\273\244\345\231\250.md" @@ -0,0 +1,40 @@ +### 配置一个过滤器 + +1. 可以通过创建一个类并实现相应的过滤器接口来实现自定义过滤器。以下是一个简单的日志记录过滤器示例 + +``` +建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(); + } + } +} + +在这个示例中,LogActionFilterAttribute 继承自 ActionFilterAttribute,并在 OnActionExecuting 和 OnActionExecuted 方法中记录了一些日志信息。可以通过在控制器或动作方法上应用 [LogActionFilter] 特性来使用这个日志记录过滤器。 + +[LogActionFilter] +public class HomeController : Controller +{ + public IActionResult Index() + { + return View(); + } +} + +``` diff --git "a/\345\217\262\347\247\200\347\272\242/20240530_\345\206\205\345\255\230\346\225\260\346\215\256 .md" "b/\345\217\262\347\247\200\347\272\242/20240530_\345\206\205\345\255\230\346\225\260\346\215\256 .md" new file mode 100644 index 0000000000000000000000000000000000000000..8d32a0a4ba7d50a924b9bd69f8ea4f278abc8696 --- /dev/null +++ "b/\345\217\262\347\247\200\347\272\242/20240530_\345\206\205\345\255\230\346\225\260\346\215\256 .md" @@ -0,0 +1,12 @@ +### 项目创建简易步骤 +一.主体结构: ①:Resultfull风格的webAApi ②:Asp.net Core 8.0 ③:采用传统控制器模式 + +二. 模型设计(图书管理系统) 作者:(Authors) 姓名:AuthorName 性别:Gender 出生年月:Birthday + +图书: 书名:BookName 出版社:Publister 作者:AuthorId 价格:Price + +三.内存型数据库(自定义的一个集合类) + +四.仓储系统(准备用来接入数据库的一套接口) ①:通用型仓储接口 ②:个性化的仓储系统(作者的增删改查,图书的增删改查) + +五.利用第一步的主体结构,具体实现Resultfull风格的关于作者,图书的CRUD \ No newline at end of file diff --git "a/\345\217\262\347\247\200\347\272\242/20240531_\351\241\271\347\233\256\350\267\257\347\224\261.md" "b/\345\217\262\347\247\200\347\272\242/20240531_\351\241\271\347\233\256\350\267\257\347\224\261.md" new file mode 100644 index 0000000000000000000000000000000000000000..be9f47f6b08467de699bddaaf4132683ca7275df --- /dev/null +++ "b/\345\217\262\347\247\200\347\272\242/20240531_\351\241\271\347\233\256\350\267\257\347\224\261.md" @@ -0,0 +1,49 @@ +接口 +实例名 Repository + + //通过Id获取所有作者的实现 + 实例对象? GetAuthorById(Guid guid); + + //获取所有作者的方法 + //函数三要素(函数名,形参,返回值) + ICollection<实例对象> GetAllAuthors(); +实例名 Repository + + 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); + } + } +控制器中 + + 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 + + public void ConfigureServices(IServiceCollection services) + { + services.AddControllers(); + //新加 + services.AddScoped(); + } \ No newline at end of file