diff --git "a/\351\237\251\345\206\260\345\206\260/2021-06-21.md" "b/\351\237\251\345\206\260\345\206\260/2021-06-21.md" index aac8b0090b419fe8638e4616007ea5143139be37..3ff0a3e38f7f1214452286469b5bbc089fb10663 100644 --- "a/\351\237\251\345\206\260\345\206\260/2021-06-21.md" +++ "b/\351\237\251\345\206\260\345\206\260/2021-06-21.md" @@ -32,8 +32,13 @@ dotnet sln add [-h|--help] dotnet sln add ApiTest.APi dotnet sln --in-root 将项目放在解决方案的根目录下,而不是创建解决方案文件夹。无法与 -s|--solution-folder 一起使用。如果省略此选项和 -s|--solution-folder,则结果与指定此选项相同。自 .NET Core 3.0 SDK起可用。 dotnet sln -s|--solution-folder 要将项目添加到的目标解决方案文件夹路径。 无法与 --in-root 一起使用。如果省略此选项和 --in-root,则结果与指定 --in-root 相同。自 .NET Core 3.0 SDK 起可用。 - +dotnet new sln dotnet new webapi --no-https +rm .\ApiTest.Data\ +dotnet new classlib +dotnet add package Newtonsoft.json +dotnet add ApiTest.Api reference ApiTest.Data +dotnet new classlib -n ApiTest.Domain dotnet run ``` diff --git "a/\351\237\251\345\206\260\345\206\260/2021-06-22.md" "b/\351\237\251\345\206\260\345\206\260/2021-06-22.md" new file mode 100644 index 0000000000000000000000000000000000000000..ac178d7dedfdf09a3f6293d6691638ae19341d84 --- /dev/null +++ "b/\351\237\251\345\206\260\345\206\260/2021-06-22.md" @@ -0,0 +1,95 @@ +# WebApi +## GET请求 +GET:从服务器检索内容或者实体在HTTP术语。GET请求通常不包含请求体,但是是被允许的。一些网络缓存设备仅仅GET方式响应。GET请求通常不会导致服务器数据变化。 +## POST +POST:用客户端提供的数据更新实体。一个POST请求通常在请求体中包含信息,这些信息在应用服务器是可以被使用的。POST请求被认为是非幂等性的,意味着如果多个请求被处理和仅仅一个请求被处理结果是不一样的。 +## PUT +PUT:添加一个由客户端提供的数据实体。一个PUT请求通常在请求体包含服务器用来创建新实体用的信息。通常,PUT请求被认为是幂等性的,意味着请求可以反复使用相同应用的结果。 +## DELETE +DELETE:删除一个基于URI内容的实体或者由客户端提供的请求体。DELETE请求通常在REST服务请求接口。 +``` +dotnet new sln +dotnet new webapi -n ApiTest.Api +dotnet sln add ApiTest.Api +dotnet new webapi --no-https或在Startup.cs注释掉app.UseHttpsRedirection(); +dotnet run -p ApiTest.Api +安装REST Client扩展 +创建一个以.http结尾的文件 +创建.NET Core环境 +``` +UserController.cs +``` +using System.Collections.Generic; +using Microsoft.AspNetCore.Mvc; +using Admin3000.Backend.Api.Entity; + +namespace Admin3000.Backend.Api.Controllers +{ + [ApiController] + [Route("[controller]")] + public class UserController: ControllerBase + { + public IEnumerable Get() + { + var users = new List{ + new Users{ + Id = 1, + Username = "沈遇", + Password = "123" + }, + new Users{ + Id = 2, + Username = "花花", + Password = "123" + }, + new Users{ + Id = 2, + Username = "辛安", + Password = "123" + }, + }; + return users; + } + [HttpGet("{id}")] + public string Get(int id) + { + return string.Format("你的id是:{0}",id); + } + [HttpPost] + public dynamic Post(dynamic model) + { + return new { + CodeStatus = 1000, + Data = model, + msg="请求成功" + }; + } + [HttpPut("{id}")] + public dynamic Put(int id,dynamic model) + { + return new{ + CodeStatus = 1000, + Data = string.Format("修改了一个指定id为{0}的用户",id), + msg="修改成功" + }; + } + [HttpDelete("{id}")] + public dynamic Delete(int id) + { + return new { + CodeStatus = 1000, + Data = string.Format("删除指定的Id为{0}的用户",id), + msg="删除成功" + }; + } + } + +} +``` +![图片无法显示](./imgs/2021-06-24_004249.png) + +![图片无法显示](./imgs/2021-06-24_004422.png) + +![图片无法显示](./imgs/2021-06-24_004458.png) + +![图片无法显示](./imgs/2021-06-24_004526.png) \ No newline at end of file diff --git "a/\351\237\251\345\206\260\345\206\260/2021-06-24.md" "b/\351\237\251\345\206\260\345\206\260/2021-06-24.md" new file mode 100644 index 0000000000000000000000000000000000000000..acc8220138b3ee0c06216b0076aee055d5582395 --- /dev/null +++ "b/\351\237\251\345\206\260\345\206\260/2021-06-24.md" @@ -0,0 +1,91 @@ +# WebApi +## 分页查询 +安装C# XML Documentation Comments和REST Client +``` +### +GET http://localhost:5000/users?pageIndex=1&pageSize=6&searchKey=7 +``` +UserController.cs +``` +using System.Collections.Generic; +using Microsoft.AspNetCore.Mvc; +using ApiTest.Api.Entity; +using System.Linq; + +namespace ApiTest.Api.Controllers +{ + [ApiController] + [Route("[controller]")] + public class UsersController:ControllerBase + { + public IEnumerable Get(string searchKey,int pageIndex=1,int pageSize=20) + { + var users = GetUsers().Where(x=>x.Username.Contains(searchKey)).Skip((pageIndex-1)*pageSize).Take(pageSize); + return users; + } + private IEnumerable GetUsers() + { + var count = 50; + var users = new List(); + for(int i=1;i<=count;i++) + { + var u = new Users{ + Id=1, + Username = string.Format("淙淙{0}",i), + Password = string.Format("密码{0}",i) + }; + users.Add(u); + } + return users; + } + } +} +``` +## ef core +安装ef core指令 +dotnet add package Microsoft.EntityFrameworkCore.Sqlite + +Entity +``` +Roles.cs +using System; +namespace ApiTest.Api.Entity +{ + public class Roles + { + public int Id{get;set;} + public int RoleName{get;set;} + public DateTime CreatedTime{get;set;} + public DateTime UpdatedTime{get;set;} + } +} + +UserRole.cs +namespace ApiTest.Api.Entity +{ + public class UserRole + { + public int Id{get;set;} + public int UserId{get;set;} + public int RoleId{get;set;} + } +} +Usser.cs +namespace ApiTest.Api.Entity +{ + public class Users + { + public int Id{get;set;} + public string Username{get;set;} + public string Password{get;set;} + } +} +``` + +创建数据库 +``` +dotnet tool install --global dotnet-ef +dotnet add package Microsoft.EntityFrameworkCore.Designdotnet ef migrations add InitialCreate +dotnet ef database update +``` +这会安装 dotnet ef 和设计包,这是对项目运行命令所必需的。 migrations 命令为迁移搭建基架,以便为模型创建一组初始表。 database update 命令创建数据库并向其应用新的迁移。 \ No newline at end of file diff --git "a/\351\237\251\345\206\260\345\206\260/2021-06-26.md" "b/\351\237\251\345\206\260\345\206\260/2021-06-26.md" new file mode 100644 index 0000000000000000000000000000000000000000..c5f72f81275d8171cbe7e37503965df60faab229 --- /dev/null +++ "b/\351\237\251\345\206\260\345\206\260/2021-06-26.md" @@ -0,0 +1,37 @@ +# 数据迁移 +1. 由定义好的数据库模型(实体类),生成数据迁移文件,命令:dotnet ef migrations add InitialCreate + +2. 由数据迁移文件,更新表结构到数据,命令:dotnet ef database update +## 控制反转和依赖注入 +区别: + 控制反转:创建对象实例的控制权从代码控制剥离到IOC容器控制,实际就是你在xml文件控制,侧重于原理。 + + 依赖注入:创建对象实例时,为这个对象注入属性值或其他对象实例,侧重于现实。 + +IOC模式,系统中通过引入实现了IOC模式的IOC容器,即可由IOC容器来管理对象的生命周期、依赖关系等,从而使得反应程序和依赖性规范与实际的应用程序代码分开。其中一个特点就是通过文本的配置文件进行应用程序组件间相互关系的配置,而不用重新修改并编译具体的代码。 + +IOC中最基本的Java技术就是“反射”编程。反射又是一个生涉的名词,通俗的说反射就是根据给出的类名(字符串)来生成对象。这种编程方式可以让对象在生成时才决定要生成哪一种对象。反射的应用是很广泛,象中都是用“反射”做为最基本的技术手段。 + +概念: + +控制反转是一个重要的面向对象编程的法则来消减计算机程序的耦合问题,也是轻量级的Spring框架的核心。控制反转一般分为两种类型,依赖注入和依赖查找。依赖注入应用比较广泛。 + +依赖注入:组件不做定位查询,只提供普通的Java方法让容器去决定依赖关系。容器全权负责的组件的装配,它会把符合依赖关系的对象通过JavaBean属性或者构造函数传递给需要的对象。通过JavaBean属性注射依赖关系的做法称为设值方法注入;将依赖关系作为构造函数参数传入的做法称为构造子注入。 + +## 使主键自增长 +``` +dotnet build +dotnet ef database update +dotnet add package System.ComponentModel +dotnet ef migrations add 修改自增长 +上行代码运行显示内容: +An operation was scaffolded that may result in the loss of data. Please review the migration for accuracy. +脚手架操作可能会导致数据丢失.请检查迁移的准确性. +dotnet ef database update +报错:To change the IDENTITY property of a column, the column needs to be dropped and recreated. +要更改列的 IDENTITY 属性,需要删除并重新创建该列. +dotnet ef migrations add 初始化自增长 +删除之前创建的数据库后,可以运行一波dotnet clean进行清理 +dotnet ef database update再刷新一哈数据库 +先F5运行起来再去数据库查询 +``` \ No newline at end of file diff --git "a/\351\237\251\345\206\260\345\206\260/2021-06-28.md" "b/\351\237\251\345\206\260\345\206\260/2021-06-28.md" new file mode 100644 index 0000000000000000000000000000000000000000..3a75bc0e3cdc0c26eb443295a5d6bea96ce46711 --- /dev/null +++ "b/\351\237\251\345\206\260\345\206\260/2021-06-28.md" @@ -0,0 +1,30 @@ +### 完整的运行代码 +``` +dotnet new sln 创建模板 +dotnet new webapi -n ApiTest.Api +cd ./ApiTest.Test/ +dotnet new webapi -n ApiTest.Api --no-https +dotnet sln add ApiTest.Api 添加到解决方案 +dotnet run -p ApiTest.Api +dotnet run 运行 +dotnet add package Microsoft.EntityFrameworkCore.Sqlserver 添加Sqlserver安装包 +dotnet add package Microsoft.EntityFrameworkCore +dotnet add package Microsoft.EntityFrameworkCore.Sqlite 添加Sqlite安装包 +dotnet tool install --global dotnet-ef 全局安装 +dotnet add package Microsoft.EntityFrameworkCore.Design 添加设计包 +dotnet add package Microsoft.AspNetCore.Authentication.JwtBearer +dotnet ef migrations add 初始化数据库 +dotnet ef database update 更新数据库 +dotnet ef migrations add 添加角色和用户角色 -p ApiTest.Api +dotnet build 还原 +dotnet ef database update +dotnet ef database drop +dotnet ef migrations add 尝试建立主外键关系 +dotnet ef database update +dotnet ef database drop +dotnet clean +dotnet build +dotnet add package System.ComponentModel +dotnet ef migration add 修改自增 +dotnet ef migrations add +``` \ No newline at end of file diff --git "a/\351\237\251\345\206\260\345\206\260/2021-06-30.md" "b/\351\237\251\345\206\260\345\206\260/2021-06-30.md" new file mode 100644 index 0000000000000000000000000000000000000000..9cbec1b0271571f01e5fa8c948aead51b800a8fe --- /dev/null +++ "b/\351\237\251\345\206\260\345\206\260/2021-06-30.md" @@ -0,0 +1,83 @@ +# 接口的实现 +IRepository.cs +``` +using Admin10.Backend.Api.Entity; +using System.Threading.Tasks; +using System.Collections.Generic; +using System.Linq; + +namespace Admin10.Backend.Api.Repository +{ + public interface IRepository where T : BaseEntity + { + IQueryable Table {get;set;} + + T GetById(object id); + + void Insert(T entity); + + Task InsertAsync(T entity); + + void InsertBulk(IEnumerable entities); + + Task InsertBulkAsync(IEnumerable entities); + + void Delete(object id); + + void DeleteBulk(IEnumerable ids); + + void Update(T entity); + + void UpdateBulk(IEnumerable entities); + void updateUser(Users user); + } +} +``` +## IRepository和IRepository +框架的重构想的最多的最重要的几个问题: + +1. 解耦(每层可以替换其它的,比如换一个UI层可以把Web项目快速转换成winform项目) + +2. 扩展性(可以灵活抹去框架的某个层,让其他的第三方框架依据自己的接口实现该层的逻辑,其它层不变,也就是插拨扩展) +3. 灵活(开发便捷,使用灵活) + +4. 维护性(别人了解框架后,可以让别人无障碍维护) + +IRepository接口定义形式(其中IEntity是一个实体类接口): +``` +public interface IRepository where T : Entity.IEntity +{ + T FindBy(string primaryKey); + IEnumerable FindAll(); + IEnumerable FindAll(string where); + IEnumerable FindAll(string where, string order); + IEnumerable FindAll(int pageIndex, int pageSize, string where, string order, out int count); + void Add(T entity); + void Delete(T entity); + void DeleteAll(); + void DeleteAll(string where); + void DeleteAll(System.Collections.IEnumerable pkValues); + void Update(T entity); + bool Exists(string primaryKey); +} +``` +IRepository和接口IEntity通过泛型T结合在了一起,形成了耦合。 + +每个IEntity的子类都得对应一个IRepository的子类 + +IRepository还是IRepository 都各自有各自的优势: +1. IRepository的子类对实体类是很专注的,它只可以操作一个实体类,对IRepository的子类的修改不会影响到其他实体类的操作 + +从而可以实现对应实体类的个性化拓展; + +2. IRepository可以操作所有的实体类,修改IRepository的子类则会影响所有的实体的操作 + +所以IRepository接口一个重要的优势是: + +在某个实体类的Server层可以统一用IRepository类的方法实现业务, + +不需要像```IRepository```实现方式一样,New额外的对象就才可以操作其他实体类, + +只要在```[Repository.方法]```里的T换成其他实体类就可以了 + +这对解耦来说是有好处的。 \ No newline at end of file diff --git "a/\351\237\251\345\206\260\345\206\260/2021-07-01.md" "b/\351\237\251\345\206\260\345\206\260/2021-07-01.md" new file mode 100644 index 0000000000000000000000000000000000000000..b7c1635539d12d7f3ed363414553dcf67b26fccd --- /dev/null +++ "b/\351\237\251\345\206\260\345\206\260/2021-07-01.md" @@ -0,0 +1,37 @@ +# ef core api +``` +dotnet restore +dotnet add package Micorsoft.EntityFrameworkCore:EF框架的核心包 +dotnet add package Micorsoft.EntityFrameworkCore.SqlServer:针对SqlServer数据库的扩展,使用SqlServer数据库必须 +dotnet add package Micorsoft.EntityFrameworkCore.Tools +dotnet add package Micorosft.EntityFrameworkCore.Design:用户根据现有的数据库生成模型代码 +dotnet add package Microsoft.EntityFrameworkCore.SqlServer.Design +``` + +生成实体及上下文对象 +``` +protected override void OnConfiguring(DbContextOptionsBuilder options) + { + options.UseSqlServer(@"server=.;database=Admin10;uid=sa;pwd=123456;"); + } +``` + +在appsettings.json文件中配置: +``` +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + }, + "AllowedHosts": "*", + "TokenParameter":{ + "secret":"123456123456", + "issuer":"Weizi", + "accessExpiration":120, + "regreshExpiration":1440 + } +} +``` \ No newline at end of file diff --git "a/\351\237\251\345\206\260\345\206\260/2021-07-03.md" "b/\351\237\251\345\206\260\345\206\260/2021-07-03.md" new file mode 100644 index 0000000000000000000000000000000000000000..a1afd8821d236ad2b3af1d84273dfd0f1af046c7 --- /dev/null +++ "b/\351\237\251\345\206\260\345\206\260/2021-07-03.md" @@ -0,0 +1,9 @@ +# 复习C# +栈:它是一个内存数组,是一个先进后出的数据结构 +栈的特征:数据只能从栈顶进,从栈顶出 +堆:它是一个内存区域,可以分配大块区域储存某类型的数据,与栈不同的是它里面的数据可以任意排序和一出 + +什么是泛型:https://www.cnblogs.com/zhouzhou-aspnet/articles/2591596.html + +复习C#知识,如图所示 +![图片无法显示](./imgs/2021-07-08_111837.png) \ No newline at end of file diff --git "a/\351\237\251\345\206\260\345\206\260/2021-07-05.md" "b/\351\237\251\345\206\260\345\206\260/2021-07-05.md" new file mode 100644 index 0000000000000000000000000000000000000000..17f324164b4f6a1e1e6905963a670d799668ad6d --- /dev/null +++ "b/\351\237\251\345\206\260\345\206\260/2021-07-05.md" @@ -0,0 +1,51 @@ +# jwt asp。 net core +## 什么是JWT +JSON Web Token(JWT)是目前最流行的跨身份验证解决方案。 + +JWT是能代表用户身份的令牌,可以使用JWT令牌在api接口中效验用户的身份以确认用户是否有访问api的权限。 + +JWT中包含了身份认证必须的参数以及用户自定义的参数,JWT可以使用秘密(使用HMAC算法)或使用RSA或ECDSA的公钥/私钥对进行签名。 + +aspnet core中自带了一个jwt帮助类,其实原理一样,对上面做了封装,丰富了一个内容,我们继续使用一个静态方法 + +## Payload 有效载荷 +Payload 部分也是一个JSON对象,用来存放实际需要传递的数据。JWT 规定了7个官方字段,供选用。 + ++ iss (issuer):签发人 ++ exp (expiration time):过期时间 ++ sub (subject):主题 ++ aud (audience):受众 ++ nbf (Not Before):生效时间 ++ iat (Issued At):签发时间 ++ jti (JWT ID):编号 + +在Startup.cs文件中的ConfigureServices方法中注入上下文对象: +``` +services.AddDbContext(o => o.UseSqlServer()); +``` + +dotnet add package Microsoft.AspNetCore.Authentication.JwtBearer +显示框架不兼容,运行下列 +dotnet add package Microsoft.AspNetCore.Authentication.JwtBearer -h +dotnet add package Microsoft.AspNetCore.Authentication.JwtBearer -v 3.1 + +在Params添加TokenParameter.cs文件: +``` +namespace Admin3000.Backend.Api.Params +{ + public class TokenParameter + { + //生成token的所需要的密钥,一定不能泄漏 + public string Secret { get; set; } + + // 发行token的发行人(可以是个人或者组织) + public string Issuer { get; set; } + + // token的有效分钟数 + public int AccessExpiration { get; set; } + + // RefreshExpiration的有效分钟数 + public int RefreshExpiration { get; set; } + } +} +``` \ No newline at end of file diff --git "a/\351\237\251\345\206\260\345\206\260/imgs/2021-06-24_004249.png" "b/\351\237\251\345\206\260\345\206\260/imgs/2021-06-24_004249.png" new file mode 100644 index 0000000000000000000000000000000000000000..a2655635f5ca5b36ed34624a9e58e847f6a9984d Binary files /dev/null and "b/\351\237\251\345\206\260\345\206\260/imgs/2021-06-24_004249.png" differ diff --git "a/\351\237\251\345\206\260\345\206\260/imgs/2021-06-24_004422.png" "b/\351\237\251\345\206\260\345\206\260/imgs/2021-06-24_004422.png" new file mode 100644 index 0000000000000000000000000000000000000000..b86f9c8626f304e4bc3a393af7f4a3f3072badb6 Binary files /dev/null and "b/\351\237\251\345\206\260\345\206\260/imgs/2021-06-24_004422.png" differ diff --git "a/\351\237\251\345\206\260\345\206\260/imgs/2021-06-24_004458.png" "b/\351\237\251\345\206\260\345\206\260/imgs/2021-06-24_004458.png" new file mode 100644 index 0000000000000000000000000000000000000000..9df5d2939f41cf095efd2194c5b72a7f1e3fd17d Binary files /dev/null and "b/\351\237\251\345\206\260\345\206\260/imgs/2021-06-24_004458.png" differ diff --git "a/\351\237\251\345\206\260\345\206\260/imgs/2021-06-24_004526.png" "b/\351\237\251\345\206\260\345\206\260/imgs/2021-06-24_004526.png" new file mode 100644 index 0000000000000000000000000000000000000000..21913a0acbce62c311e58d3f3dfd36ae1784e2be Binary files /dev/null and "b/\351\237\251\345\206\260\345\206\260/imgs/2021-06-24_004526.png" differ diff --git "a/\351\237\251\345\206\260\345\206\260/imgs/2021-07-08_111837.png" "b/\351\237\251\345\206\260\345\206\260/imgs/2021-07-08_111837.png" new file mode 100644 index 0000000000000000000000000000000000000000..b163d3a273b19daf435d1ec300adf1462269072f Binary files /dev/null and "b/\351\237\251\345\206\260\345\206\260/imgs/2021-07-08_111837.png" differ