diff --git "a/\347\231\275\345\251\211\345\251\267/20240524.md" "b/\347\231\275\345\251\211\345\251\267/20240524.md" deleted file mode 100644 index a859c966ceedc5e75b270c82e1298fff092e5b98..0000000000000000000000000000000000000000 --- "a/\347\231\275\345\251\211\345\251\267/20240524.md" +++ /dev/null @@ -1,2 +0,0 @@ -## >?? -## src形式 \ No newline at end of file diff --git "a/\347\231\275\345\251\211\345\251\267/20240524api\346\234\200\347\256\200\345\206\231\346\263\225.md" "b/\347\231\275\345\251\211\345\251\267/20240524api\346\234\200\347\256\200\345\206\231\346\263\225.md" new file mode 100644 index 0000000000000000000000000000000000000000..3e4f4573c9aa5d9edc14714c8e092cbe1e168c38 --- /dev/null +++ "b/\347\231\275\345\251\211\345\251\267/20240524api\346\234\200\347\256\200\345\206\231\346\263\225.md" @@ -0,0 +1,56 @@ +## 最简形式 +## src形式 +1. 更换URL + Properties\launchSettings.json → profiles + DemoOne\DemoOne.http → + @url = http://localhost:3000 + GET {{{{url}}}}/weatherfore +2. 新建Statup.cs + 1. 新建Startup 类型定义 + namespace DemoOne; + + public class Startup + { + public void Configure(IApplicationBuilder app) + { + app.UseRouting(); + app.UseEndpoints(endpoints=>endpoints.MapControllers()); + } + + public void ConfigureServices(IServiceCollection services) + { + services.AddControllers();//注册服务 + } + } +3. Program.cs + 1. 新建Program 类型 + 2. 新建Main 入口 + namespace DemoOne; + public static class Program + { + public static void Main(string[] args) + { + CreateHostBuilder(args).Build().Run(); + } + + private static IHostBuilder CreateHostBuilder(string[] args) + { + return Host.CreateDefaultBuilder(args).ConfigureWebHostDefaults(Builder => + { + Builder.UseStartup(); + }); + } + } +4. 新建Controllers → BlogsController.cs 新建路由管理 + using Microsoft.AspNetCore.Mvc; + namespace DemoOne; + + [Route("[controller]")] + public class BlogsController: ControllerBase + { + public IActionResult Index() + { + return Ok("one"); + } + } + \ No newline at end of file diff --git "a/\347\231\275\345\251\211\345\251\267/20240527.md" "b/\347\231\275\345\251\211\345\251\267/20240527.md" deleted file mode 100644 index 56807bab088253fc2e79f67d2a5317a0cc13b01e..0000000000000000000000000000000000000000 --- "a/\347\231\275\345\251\211\345\251\267/20240527.md" +++ /dev/null @@ -1,230 +0,0 @@ -一、路由 -1.概念 - -根据路由配置确定如何处理该请求。路由定义了不同 URL 地址对应的控制器和操作方法 -2.示例 - -[Route("[controller]")] //定义控制器的路由前缀,即/blogs -// BlogsController类继承自ControllerBase,它是一个用于构建API的基类(不包含视图支持) -public class BlogsController : ControllerBase -{ - // 处理对/blogs的GET请求 - public IActionResult Index(){ - return Ok("999"); //返回一个包含字符串"999"的200 OK响应 - } - - [Route("{id}")] //定义Single方法的路由模板 - // 处理对/blogs/{id}的GET请求,接受一个整型参数id - public IActionResult Single(int id) - { - return Ok(id); //返回一个包含id值的200 OK响应 - } -} - -// 上面两个函数可以合并写为以下形式: - [Route("{id?}")] - public IActionResult Index(){ - // 代码块 - } - public IActionResult Single(int id) - { - // 代码块 - } - -3.拓展:关于控制器 - - 控制器是应用程序负责处理用户请求并生成响应的类 - 本身带有多个action方法,而每个action方法对应着一个特定的HTTP请求类型,可以接受参数、调用服务、查询数据库等等 - 通常继承自ControllerBase类(不包含视图)或Controller类 - -二、模型绑定 -1.概念 - - 一旦根据路由确定要调用的操作方法,就会尝试从请求中提取数据,将其绑定到操作方法的参数或模型对象上 - 模型绑定不仅可以处理基本的数据类型,还可以解析复杂的数据 - -2.Action特性 - - [FromQuery]:从HTTP请求的查询字符串中获取参数的值 - [FromForm]:从表单中获取参数的值 - [FromHeader]:从HTTP 请求的头部信息中获取参数值 - [FromBody]:从请求的内容体获取参数值 - [FromServices]:从依赖注入容器中获取参数值 - [FromRoute]:从路由中获取参数值 - [BindRequiredAttribute]:如果没有值绑定到此参数,或绑定不成功,这个特性将添加一个ModelState错误 - -// 这里需要下载包 -using Microsoft.AspNetCore.Mvc.ModelBinding; -namespace Api; -public class BlogDto -{ - [BindRequired] //强调该值必须填写 - public string Title {get;set;}; -} - - [BindNever]:在进行模板绑定时,忽略此参数 - [ApiController]:位于命名空间下方,会尝试自动获取参数值,不再需要为参数指定上述其他特性 - -3.示例 - - // Dto/BlogDto.cs - public class BlogDto - { - public string Title {get;set;} = null!; - public string Author {get;set;} = null!; - public string? Flag {get;set;} - } - // BlogsController.cs - [HttpPost] - public ActionResult Post(BlogDto blogDto) - { - return blogDto; - } - -4.拓展:什么是VO、DTO、DO、PO - -VO、DTO、DO、PO 是常见的命名约定,通常用于表示不同的数据对象或数据传输对象 - - VO(View Object):视图对象,用于展示层,它的作用是把某个指定页面(或组件)的所有数据封装起来 - DTO(Data Transfer Object):数据传输对象,用于在不同层之间传输数据 - DO(Domain Object):领域对象,从现实世界中抽象出来的有形或无形的业务实体,例如商品、订单、用户等 - PO(Persistent Object):持久化对象,数据需要被保存到持久存储介质(比如数据库、文件系统等)中,PO就是用来表示这些持久化数据的对象 - -三、模型验证 - -是指数据被使用之前的验证过程,它发生在模型绑定之后 -1.数据注解 - -使用Required特性为属性添加需要的数据验证 - - using System.ComponentModel.DataAnnotations; - namespace Api; - public class BlogDto - { - [MinLength(2,ErrorMessage="标题不能少于2个字符")] - public string Title {get;set;} = null!; - [Required(ErrorMessage="作者不能为空")] - public string Author {get;set;} = null!; - public string? Flag {get;set;} - } - - // BlogsController.cs - // 检查对象是否满足指定的条件 - public ActionResult Put(int id,BlogDto blogDto) - { - if (!ModelState.IsValid){ - return BadRequest(ModelState); - }else - { - - return Ok(new { id, blogDto }); - } - } - -2.继承自ValidationAttribute类的自定义验证 - - // Atrribute/NoSpaceAttribute.cs - using System; - using System.ComponentModel.DataAnnotations; - - public class NoSpaceAttribute : ValidationAttribute - { - protected override ValidationResult IsValid(object value, ValidationContext validationContext) - { - if (value != null && value.ToString().Contains(" ")) - { - // 返回如果包含空格的错误验证消息 - return new ValidationResult("字符串不能包含空格"); - } - else - { - // 返回成功验证结果 - return ValidationResult.Success; - } - } - } - // 在属性中使用 - [NoSpace] - public string Author {get;set;} = null!; - -3.使用Model实现IValidatableObject接口自定义验证 - - using System; - using System.Collections.Generic; - using System.ComponentModel.DataAnnotations; - - namespace Api - { - public class BlogDto : IValidatableObject - { - public string Title { get; set; } = null!; - - public string Author { get; set; } = null!; - public string? Flag { get; set; } - - public IEnumerable Validate (ValidationContext validationContext) - { - if (IsContainSpace(Title)) - { - yield return new ValidationResult("标题不能包含空格", new[] { nameof(Title) }); - } - } - - private bool IsContainSpace(string value) - { - return value?.Contains(" ") ?? false; - } - } - } - -四、执行控制器方法Action -1.概念 - - 一个控制器包括一个或多个Action,而Action是控制器中的一些public类型的函数,它们可以接受参数、执行相关逻辑,最终返回一个结果,该结果作为HTTP响应返回给客户端 - 如果要让一个Action不起作用,在它上方添加[NonAction] - -2.常见的返回结果类型 - -Action根据实际需求,可以接受参数也可以不接受,可以返回任何类型的值 -1.状态码 - - Ok():操作成功200 - BadRequest():错误请求400 - NoContent():操作成功都是不返回任何内容204 - NotFound():资源找不到404 - Unauthorized():未授权401 - -2.包含对象的状态码 - -这一类结果继承自ObjectResult,包括OkObjectResult、CreateResult和NotObjectResult等等 - - [HttpPut("{id}")] - public ActionResult Put(int id,BlogDto blogDto) - { - var res = new ObjectResult(new {id,blogDto}); - return res; - } - -3.重定向结果 - - // 重定向到指定url - return Redirect("http://www.baidu.com/"); - // 重定向到当前应用程序中的另一个url - return LocalRedirect("/blogs/users"); // Location: /blogs/users - // 重定向到指定Action - return RedirectToAction("index"); // Location: /Blogs - // 重定向到指定的路由 - return RedirectToRoute("default", new { controller = "Blogs", action = "Details" }); - -4.内容结果 - - // 返回视图ViewResult、PartialViewResult - // 返回JSON字符串 - return new JsonResult(new { - msg = "This is a JSON result", - data = blogDto - }); - // Content返回一个字符串 - return Content("我是添加数据"); - -此外还可以使用ActionResult类,即可以表示ActionResult对象,也可以表示一个具体类型(由泛型参数T指定) diff --git "a/\347\231\275\345\251\211\345\251\267/20240527mvc\346\250\241\345\274\217\350\267\257\347\224\261.md" "b/\347\231\275\345\251\211\345\251\267/20240527mvc\346\250\241\345\274\217\350\267\257\347\224\261.md" new file mode 100644 index 0000000000000000000000000000000000000000..1078cf3ff6ad4cbd4b26ae68f37eef7cfd7e5bcd --- /dev/null +++ "b/\347\231\275\345\251\211\345\251\267/20240527mvc\346\250\241\345\274\217\350\267\257\347\224\261.md" @@ -0,0 +1,120 @@ +## 访问路由路径 +[Route("/api/[controller]")] +controller 为控制器名称 +## 路由 通过某个类型访问特定函数 + +## Dto 数据传输对象 +保存在数据库当中,保存在文件当中 +不会直接使用数据库模型作为Dto + +## get请求 + BlogsController.cs + [Route("/api/[controller]")] + public class BlogsController : ControllerBase + { + [Route("/ccc/z")] + public IActionResult Index() + { + return Ok("one"); + } + [Route("{id}")] + public IActionResult Details(int id) + { + return Ok(id); + } + } + DemoOne.http + GET {{url}}/api/blogs HTTP/1.1 + GET {{url}}/api/blogs/999 HTTP/1.1 + +## post请求 + BlogsController.cs + [Route("/api/[controller]")] + public class BlogsController : ControllerBase + { + [HttpPost] + public ActionResult Post(BlogDto blogDto) + { + return Ok(blogDto); + } + } + BlogDto.cs + namespace DemoOne; + public class BlogDto + { + public string Title { get; set; }=null!; + public string Author { get; set;}=null!; //不能为空 + public string? Flag { get; set; } //可以为空 + + } + DemoOne.http + POST {{url}}/api/blogs HTTP/1.1 + Content-Type: application/json + + { + "Title":"hello", + "Author":"zzz", + "Flag":"1 " + } +## put请求 + BlogsController.cs + [Route("/api/[controller]")] + public class BlogsController : ControllerBase + { + [HttpPost] + public ActionResult Post(BlogDto blogDto) + { + return Ok(blogDto); + } + } + DemoOne.http + put {{url}}/api/blogs HTTP/1.1 + Content-Type: application/json + + { + "Title":"hello", + "Author":"zzz", + "Flag":"1 " + } +## delete删除 + BlogsController.cs + [Route("/api/[controller]")] + public class BlogsController : ControllerBase + { + [HttpDelete] + public IActionResult Edlete(int id) + { + return Ok(id); + } + } + DemoOne.http + DELETE {{url}}/api/blogs/66 Http/1 + +## 关于Action的返回值 p65 +定义在控制器当中的公用方法 可以返回任何类型的值 + 例:返回字符串 + [HttpPut("{id}")] + public string Put(int id, BlogDto blogDto) + { + return JsonSerializer.Serialize(new { id, blogDto }) ; + } +本质是一个函数,函数的三大本质是方法的名称,参数,返回值。 +## 返回内容的主流形式 + { + code:1000, + msg:'请求成功', + data:[] + } + +## 模型绑定 + +1. [FromHeader]特性:从HTTP请求的Header中获取参数的值。 +2. [FromQuery]特性:从查询字符串中获取参数的值。 +3. [FromService]特性:从依赖注入容器中获取参数的值。 +4. [FromRouter]特性:从路由中获取参数的值。 +5. [FromFrom]特性:从表单中获取参数的值。 +6. [FromBody]特性:从HTTP请求的消息正文中获取参数的值。 + +## sku / spu +1. spu +2. sku 从属于spu diff --git "a/\347\231\275\345\251\211\345\251\267/20240528.md" "b/\347\231\275\345\251\211\345\251\267/20240528.md" deleted file mode 100644 index 36950a8039c1148f937acbe8cc3a14de7b66c790..0000000000000000000000000000000000000000 --- "a/\347\231\275\345\251\211\345\251\267/20240528.md" +++ /dev/null @@ -1,179 +0,0 @@ -五、过滤器 -1.概念 - -过滤器和中间件相似,能够在某些功能前后执行,通常用于执行日志记录、身份验证、授权、异常处理和结果转换等操作 -2.过滤器类型 - - 授权过滤器(Authorization Filter):最先执行,用于检查用户是否有权访问资源,并在未经授权时拦截请求,实现IAsyncAuthorizationFilter或IAuthorizationFilter接口 - 资源过滤器(Resource Filter):在Authorization后执行,并在其他过滤器之前和之后执行,实现IAsyncResourceFilter或IResourceFilter接口 - 动作过滤器(Action Filter):在执行Action之前或之后执行自定义逻辑,而且在模型绑定后执行,实现IAsyncActionFilter或IActionFilter接口 - 异常过滤器(Exception Filter):用于捕获并处理动作方法中抛出的异常,实现IAsyncExceptionFilter或IExceptionFilter接口 - 结果过滤器(Result Filter):在IActionResult执行的前后执行,能够控制Action的执行结果,比如格式化结果等。需要注意的是,它只有在Action方法成功执行完成后才会运行,实现IAsyncResultFilter或IResultFilter接口 - -3.示例 - - // 对于每次请求都需要设置return结果,这里可以使用Result过滤器格式化结果 - // 1. Dto/ApiResult.cs - public class ApiResult - { - public int Code { get; set; } - public string? Msg { get; set; } - public object Data { get; set; } - } - // 2. Filters/ApiResultFilter.cs 实现接口 - using Microsoft.AspNetCore.Mvc; - using Microsoft.AspNetCore.Mvc.Filters; - using System.Threading.Tasks; - namespace Api.Filters; - public class ApiResultFilter:IAsyncResultFilter{ - public async Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next) - { - // 判断返回结果是否为内容,如果是则给context.Result赋值一个新的实例对象ApiResult - if (context.Result is ObjectResult objectResult) - { - context.Result = new ObjectResult(new ApiResult - { - Code = 1000, - Msg = "请求成功", - Data = objectResult.Value - }); - } - else - { - context.Result = new ObjectResult(new ApiResult { Code = 4000, Msg = "无效请求" }); - } - // 必须记得调用,否则不能执行下一个Action - await next(); - } - } - // 3. Startup.cs 注册服务 - public void ConfigureServices(IServiceCollection services) - { - // 全局过滤 - services.AddControllers(options => { - options.Filters.Add(); - }); - } - -六、配置 -1.概念 - - 采用键值对来表示配置项,并且支持多种形式的配置源,包括文件(支持JSON、XML和INI格式)、命令行参数、环境变量、.NET内存对象等等 - 要访问配置,需要使用ConfigurationBuilder类,位于Microsoft.Extensions.Configuration命名空间下,实现IConfigurationBuilder接口,该接口包括两个重要的方法 - - public interface IConfiguration - { - // Add添加不同形式的配置源 - IConfigurationBuilder Add(IConfigurationSource source); - // Build把配置源生成为程序可访问的配置项 - IConfigurationRoot Build(); - } - -2.访问JSON配置文件 - - // appsetting.json - { - "MySettings": { - "Setting1": "Value1", - "Setting2": "Value2" - } - } - // startup.cs - // IConfiguration 用于读取应用程序的配置 - private readonly IConfiguration _configuration; - public Startup(IConfiguration configuration) - { - _configuration = configuration; - } - public void Configure(IApplicationBuilder app) - { - app.UseRouting(); - app.UseEndpoints(endpoints => { - endpoints.MapControllers(); - - }); - } - public void ConfigureServices(IServiceCollection services) - { - // 输出配置值并打印到控制台 - var setting1 = _configuration["MySettings:Setting1"]; - Console.WriteLine($"Setting1: {setting1}"); - } - -3.访问其他配置源 - - 使用AddXmlFile添加XML文件 - 使用AddIniFile添加Ini文件 - 使用AddInMemoryCollection添加KeyValuePair类型的集合,键和值的类型均为字符串 - AddEnvronmentVariables添加当前系统的所有环境变量 - -七、日志 -1.概念 - - 日志不会为应用程序增加实质性的功能,但是能够让开发人员跟踪程序的运行、调试程序以及记录错误信息 - 日志两种类型: - 系统日志:系统在运行时向外输出的记录信息 - 用户记录日志:开发人员在程序中适当的位置调用与日志功能相关的API输出 - 由以下接口组成:(都位于Microsoft.Extensions.Logging命名空间下 - Ilogger:实际执行记录日志操作的方法 - IloggerProvider:用于创建ILogger对象 - IloggerFactory:通过ILoggerProvider对象创建ILogger对象 - 日志级别: - Trace:级别最低 - Debug:用于记录调试信息 - Information:用于记录应用程序的执行流程信息 - Warning:用于记录应用程序出现的轻微错误或其他不会导致程序停止的警告信息 - Critical:严重级别最高,用于记录引起应用程序崩溃、灾难性故障等信息 - -2.Serilog - -Serilog 是一款广受欢迎的日志库,它提供了强大的日志记录功能 -1.安装 - -dotnet add package Serilog - -2.配置Serilog - - public class Program - { - public static void Main(string[] args) - { - // 设置 Serilog 日志配置 - Log.Logger = new LoggerConfiguration() - .WriteTo.Console() //日志输出到控制台 - .CreateLogger(); //创建并初始化 Log.Logger 静态实例 - CreateWebHostBuilder(args).Build().Run(); - } - public static IWebHostBuilder CreateWebHostBuilder(string[] args) - { - return WebHost.CreateDefaultBuilder(args) - .UseSerilog() // 将 Serilog 集成到 ASP.NET Core - .UseStartup(); - } - } - -3.记录日志 - - namespace Api.Controllers - { - [ApiController] - [Route("[controller]")] - public class LogController : ControllerBase - { - // 声明了一个只读的 _logger 字段,用于记录日志 - private readonly ILogger _logger; - // 构造函数通过依赖注入获取 - public LogController(ILogger logger) - { - _logger = logger; - } - - [HttpGet] - public IActionResult Index() - { - // 记录一条信息级别的日志,表示访问了首页 - _logger.LogInformation("访问了首页"); - return Ok("Welcome to the homepage"); - } - } - } \ No newline at end of file diff --git "a/\347\231\275\345\251\211\345\251\267/20240528mvc\346\216\247\345\210\266\345\231\250\350\277\207\346\273\244\345\231\250\346\227\245\345\277\227.md" "b/\347\231\275\345\251\211\345\251\267/20240528mvc\346\216\247\345\210\266\345\231\250\350\277\207\346\273\244\345\231\250\346\227\245\345\277\227.md" new file mode 100644 index 0000000000000000000000000000000000000000..e547d792654b15d95342f821591c3cbedf90b53d --- /dev/null +++ "b/\347\231\275\345\251\211\345\251\267/20240528mvc\346\216\247\345\210\266\345\231\250\350\277\207\346\273\244\345\231\250\346\227\245\345\277\227.md" @@ -0,0 +1,141 @@ +## RouterController : ControllerBase + 可以自定义但是不建议使用关键字 + +## --- 20240527 --- +1. 路由 + 1. 在控制器上写两个特性 + [ApiControllor] + [Route("/api/...")] + 2. 如果不符合restfull风格的路由的话,在action上单独写路由 + [HttpGet("/api/....")] +2. 关于匹配到函数的处理 + 1. 入参:函数的参数,模型绑定 + 2. 出参:返回的响应 + 1. 状态码 + 2. 带对象的状态码 + 3. 重定向 + 4. 内容{ + code:1000, + data:[], + msg:'请求成功' + } +## 控制器的基本写法 + 1. ApiResultFilter.cs + using Microsoft.AspNetCore.Mvc; + namespace DemoOne.Controllers + { + [ApiController] + [Route("[controller]")] + public class MyController : ControllerBase + { + // GET: /My + [HttpGet] + public IActionResult Get() + { + // 逻辑代码 + return Ok(new { Message = "Hello from MyController!" }); + } + + // GET: /My/5 + [HttpGet("{id}")] + public IActionResult Get(int id) + { + // 逻辑代码 + return Ok(new { Message = $"You requested item with id {id}" }); + } + + // POST: /My + [HttpPost] + public IActionResult Post([FromBody] MyData data) + { + // 逻辑代码 + return CreatedAtAction(nameof(Get), new { id = data.Id }, data); + } + } + 2. ApiResult.cs + public class ApiResult + { + public int Id { get; set; } + public string Content { get; set; } + } + } + + + +## 过滤器注册使用 处理成功才有结果 + 1. ApiResultFilter.cs + public class ApiResultFilter : IAsyncResultFilter + { + public async Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next) + { + //判断返回结果是否是内容,是返回Context.Result赋一个新的对象ApiResult + //判断是不是objectresult类型 + if(context.Result is ObjectResult objectResult) + { + /* var objectResult =(ObjectResult)context.Result; */ + + context.Result =new ObjectResult (new ApiResult + { + Code=1000, + Msg="yesss", + Data=objectResult.Value + }); + } + else + { + context.Result = new ObjectResult(new ApiResult {Code=1000}); + } + await next(); //理解为中间件,需要注册为服务 + } + } +2. Startup.cs + public class Startup + { + public void Configure(IApplicationBuilder app) + { + app.UseRouting(); + app.UseEndpoints(endpoints=>endpoints.MapControllers()); + } + + public void ConfigureServices(IServiceCollection services) + { + services.AddControllers(options=>{ + options.Filters.Add(); + });//注册服务 + /* services.AddScoped();//全局生效 */ + } + } + +## 获取appsetting的链接字符串,注入数据库的上下文服务 +1. appsettings.json文件中添加数据库的链接字符串 + { + "AllowedHosts": "*", + "ConnectionStrings": { + "mssql":"", + "pgsql":"" + } + } +2. Startup.cs文件配置引用 + public class Startup + { + public void ConfigureServices(IServiceCollection services) + { + services.AddControllers(options=>{ + options.Filters.Add(); + });//注册服务 + _configuration.GetConnectionString("mssql"); + //获取appsetting的链接字符串,注入数据库的上下文服务 + } + } + +## 日志serilog +1. 持久化 插入数据库 +2. 打印到控制台 + + +## +1. 实现一个全局生效的action过滤器 +2. 将Serilog集成到目前的api项目当中 + 1. 配置文件中设置可配置项 + 2. 日志写入数据库 + 3. 日志输出到控制台 \ No newline at end of file diff --git "a/\347\231\275\345\251\211\345\251\267/20240530.md" "b/\347\231\275\345\251\211\345\251\267/20240530.md" index c6b383b28ded78fa1f8202a2aeadc4096f964ce1..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 100644 --- "a/\347\231\275\345\251\211\345\251\267/20240530.md" +++ "b/\347\231\275\345\251\211\345\251\267/20240530.md" @@ -1,70 +0,0 @@ - - 入口程序 - - using Microsoft.AspNetCore; - // 1. 命名空间声明 - namespace Api - { - // 2. 类声明 - // Program类是应用程序的入口点,应用程序通常从Main方法开始执行 - public class Program - { - public static void Main(string[] args) - { - CreateWebHostBuilder(args).Build().Run(); - // CreateWebHostBuilder(args)方法被调用来创建一个IWebHostBuilder对象 - // Build()方法被调用在IWebHostBuilder对象上,生成一个IWebHost对象 - // Run()方法启动Web主机,使应用程序开始监听Web请求 - } - public static IWebHostBuilder CreateWebHostBuilder(string[] args) - { - return WebHost.CreateDefaultBuilder(args).UseStartup(); - // CreateWebHostBuilder方法用于配置和创建一个Web主机生成器 - // UseStartup()方法指定Startup类作为应用程序的启动类 - } - } - } - - 启动类 - - // Startup类是ASP.NET Core应用程序启动时调用的类,用于配置应用程序服务和HTTP请求管道 - public class Startup - { - // 用于添加和注册中间件 - public void Configure(IApplicationBuilder app) - { - app.UseRouting(); //添加路由中间件 - // 配置终结点路由 - app.UseEndpoints(endpoints => { - endpoints.MapControllers(); - // 添加控制器终结点到请求管道中,这使得控制器能够处理HTTP请求 - }); - } - // 注册准备依赖注入的服务 - // ConfigureServices方法用于配置依赖注入容器,添加应用程序所需的服务 - public void ConfigureServices(IServiceCollection services) - { - services.AddControllers(); //添加MVC控制器相关的服务到依赖注入容器中 - } - } - - 控制器 - - namespace Api.Controllers; - - [Route("[controller]")] //定义控制器的路由前缀,即/blogs - // BlogsController类继承自ControllerBase,它是一个用于构建API的基类(不包含视图支持) - public class BlogsController : ControllerBase - { - // 处理对/blogs的GET请求 - public IActionResult Index(){ - return Ok("999"); //返回一个包含字符串"999"的200 OK响应 - } - - [Route("{id}")] //定义Single方法的路由模板 - // 处理对/blogs/{id}的GET请求,接受一个整型参数id - public IActionResult Single(int id) - { - return Ok(id); //返回一个包含id值的200 OK响应 - } - } \ No newline at end of file diff --git "a/\347\231\275\345\251\211\345\251\267/20240603.md" "b/\347\231\275\345\251\211\345\251\267/20240603.md" new file mode 100644 index 0000000000000000000000000000000000000000..39a45a29c39573ee563c13acfaa6559015058ada --- /dev/null +++ "b/\347\231\275\345\251\211\345\251\267/20240603.md" @@ -0,0 +1,8 @@ + +Dto的好处 + + 数据封装与传输:DTO用于在不同层之间传递数据,特别是在控制器和服务层之间。它能够将请求中的数据进行封装,从而便于传输。 + 解耦:通过使用DTO,可以避免直接暴露数据库模型(这里指Domain中的文件),从而降低各层之间的耦合度。 + 数据验证与格式化:DTO可以添加数据注释和验证属性,确保传入的数据符合预期格式和约束。这样可以在数据到达业务逻辑层之前进行初步验证。 + 安全性:可以避免将敏感的数据库字段直接暴露给客户端。 + 数据转换:以用于将复杂的数据结构转换为适合传输的简单结构,或者将客户端传入的数据转换为适合业务逻辑处理的结构。 diff --git "a/\347\231\275\345\251\211\345\251\267/20240605.md" "b/\347\231\275\345\251\211\345\251\267/20240605.md" new file mode 100644 index 0000000000000000000000000000000000000000..7003b211d8af70c0995736e97918c8c1a4a4954d --- /dev/null +++ "b/\347\231\275\345\251\211\345\251\267/20240605.md" @@ -0,0 +1,2 @@ +## 配置接口 +## 实现 \ No newline at end of file diff --git "a/\347\231\275\345\251\211\345\251\267/20240606.md" "b/\347\231\275\345\251\211\345\251\267/20240606.md" new file mode 100644 index 0000000000000000000000000000000000000000..d1600f9adb8bea85893ed96b8ab1427e2ed930bd --- /dev/null +++ "b/\347\231\275\345\251\211\345\251\267/20240606.md" @@ -0,0 +1,47 @@ + +一、EF Core的简介 +1.概念 + + EF Core(Entity Framework Core)是基于.NET Core的轻量级ORM框架 + ORM能够处理数据库与高级编程语言中对象之间的映射关系 + +2. .NET对象与关系型数据库的对应关系 +.NET对象 关系型数据库 +类 表 +类的属性或字段 表中的列 +集合中的元素 表中的行 +对于其他类的引用 外键 +3.支持LINQ(集成语言查询) +1.查询所有 + +var result = from item in collection + select item; + +2.过滤 + +var result = from item in collection + where price > 10 + select item; + +3.排序 + +var result = from item in collection + orderby item.Property ascending/descending + select item; +//ascending(升序,默认);descending(降序)关键字 + +4.连接查询 + +var query = from person in people + join address in addresses on person.AddressId equals address.Id + select new { person.Name, address.City }; + +二、在项目中添加EF Core +1.安装包 + +dotnet add package Microsoft.EntityFrameworkCore + +2.安装提供程序 + +//这里为SQL Server数据库为例 +dotnet add package Microsoft.EntityFrameworkCore.SqlServer \ No newline at end of file diff --git "a/\347\231\275\345\251\211\345\251\267/20240607.md" "b/\347\231\275\345\251\211\345\251\267/20240607.md" new file mode 100644 index 0000000000000000000000000000000000000000..40bb7fd3c7b5b15f63799dbfad9c0d595a9b17b2 --- /dev/null +++ "b/\347\231\275\345\251\211\345\251\267/20240607.md" @@ -0,0 +1,10 @@ +一、EF Core的两种使用方法 +1.代码优先(推荐) + + 根据先创建好的实体类来创建数据库和表 + EF Core会将对实体类的修改同步到数据库中,都是对数据库手工修改将会在EF Core同步数据后丢失 + 用于同步代码到数据库的方法是迁移(Migration),就是提供以增量的方式来修改数据库和表结构,使实体类和数据库保持一致 + +2.数据库优先 + +根据先创建好的数据库生成相应的代码 \ No newline at end of file