diff --git "a/\351\231\210\344\276\235\346\254\243/20241122--mvc\344\274\240\345\217\202.md" "b/\351\231\210\344\276\235\346\254\243/20241122--mvc\344\274\240\345\217\202.md" index d18cb36f0b13d72b0a5a78b59082214dc882d2cd..2095a6f94c69383da73895814eaed10e1e9c9422 100644 --- "a/\351\231\210\344\276\235\346\254\243/20241122--mvc\344\274\240\345\217\202.md" +++ "b/\351\231\210\344\276\235\346\254\243/20241122--mvc\344\274\240\345\217\202.md" @@ -8,9 +8,133 @@ | 用户界面 | Tabby和xShell提供了更丰富的图形用户界面,而OpenSSH和PuTTY则更倾向于命令行界面。 | | 功能丰富度 | Tabby集成了多种网络工具,功能最丰富;xShell和PuTTY提供了一些额外的配置选项;OpenSSH则更专注于SSH连接。 | | 自动化和脚本 | OpenSSH由于是命令行工具,更适合集成到脚本和自动化任务中。 | -| 成本 | OpenSSH和PuTTY是免费的,xShell提供免费和付费版本,Tabby提供免费版本和一些付费功能。 |别总结: -2. 更新软件源,命令:apt update; 更新软件和补丁,命令:apt upgrade - - - 同步本地软件包索引与软件源服务器上的索引。软件源(repositories)是存放软件包的服务器,它们包含了软件包的元数据和文件。 - - 升级软件包: apt upgrade 命令会将本地已安装的软件包升级到最新版本。-y 参数是自动回答“yes”来确认安装,这样不需要手动确认每个软件包的升级。 + +2. 更新软件源,命令:apt update; 更新软件和补丁 +3. 命令:apt upgrade -y + - 同步本地软件包索引与软件源服务器上的索引。-y 参数是自动回答“yes”来确认安装,这样不需要手动确认每个软件包的升级。 +5. ![20241201144112](/up-z0.qiniup.com20241201144112.png) +6. +- one.cyx123.cn +- n2.cyx123.cn +- n3.cyx123.cn +- n4.cyx123.cn +- n5.cyx123.cn +- n6.cyx123.cn +- n7.cyx123.cn +- n8.cyx123.cn +- n9.cyx123.cn +- n10.cyx123.cn + +7. +![20241201143944](/up-z0.qiniup.com20241201143944.png) +![20241201144002](/up-z0.qiniup.com20241201144002.png) + +## 8 +1. ![20241201144235](/up-z0.qiniup.com20241201144235.png) +2. 3. ![20241201144317](/up-z0.qiniup.com20241201144317.png) +4. ![20241201144355](/up-z0.qiniup.com20241201144355.png) +6. ![20241201144415](/up-z0.qiniup.com20241201144415.png) +7. ![20241201144525](/up-z0.qiniup.com20241201144525.png) +8. ![20241201144600](/up-z0.qiniup.com20241201144600.png) +11. ![20241201144643](/up-z0.qiniup.com20241201144643.png) + +## 控制器传参 +1. 简单参数传递 在一个叫Blog控制器中,定义一个叫Index的Action,并且传递一个int类型的值,id为变量名 +``` + public IActionResult Index(int id){ + return Content(id.ToString()); + } +``` + +2. 简单参数传递 在一个叫Blog控制器中,定义一个叫Index_2的Action,并且传递一个string类型的值,id为变量名 +``` + public IActionResult Index_2(string id){ + return Content(id); + } +``` + +3. 简单参数传递 在一个叫Blog控制器中,定义一个叫Index_3的Action,并且传递一个string类型的值,name为变量名 +``` + public IActionResult Index_3(String name){ + return Content(name); + } +``` + +4. 复杂参数传递 在一个叫Blog的控制器中,定义一个名为Create的Action,并且传递一个BlogCreateDto类型的值,blogCreateDto为变量名 +``` + [HttpPost] + public IActionResult Create([FromBody]BlogCreateDto blogCreateDto){ + return View(blogCreateDto.Author); + } + + public IActionResult Create(int id){ + var blogCreateDto = new BlogCreateDto{ + Title = "背对背拥抱", + Author = "林俊杰", + Content = "话总说不清楚" + }; + return View(blogCreateDto); + } + + public class BlogCreateDto{ + public string Title {get;set;} = null!; + public string Author{get;set;} = null!; + public string Content{get;set;} = null!; +} +``` + + + +复杂参数传递 在一个叫Blog的控制器中,定义一个名为Create_1的Action,并且传递一个Products类型的值,productCreateDto为变量名 +``` + [HttpPost] + public IActionResult Create_1([FromBody]Product productCreateDto) + { + return View(productCreateDto.Name); + } + + public IActionResult Create_1(int id){ + var Products = new Product{ + Name = "香蕉", + Price = 10, + Stock = "加拿大" + }; + return View(Products); + } + + public class Product{ + public string Name {get;set;} = null!; + public int Price{get;set;} = 0!; + public string Stock{get;set;} = null!; +} +``` + + +复杂参数传递 在一个叫Blog的控制器中,定义一个名为Create_2的Action,并且传递一个Students类型的值,studentCreateDto为变量名 +``` + [HttpPost] + public IActionResult Create_2([FromBody] Students studentCreateDto) + { + return Content(studentCreateDto.StudentName); + } + + public IActionResult Create_2(int id){ + var student = new Students{ + StudentName = "香蕉", + Age = 50, + Sex = "女" + }; + return View(student); + } + + public class Students + { + public string StudentName{get;set;} = null!; + public string Sex{get;set;} = null!; + public int Age{get;set;} = 0!; + } +``` + + diff --git "a/\351\231\210\344\276\235\346\254\243/20241122\344\275\234\344\270\232.docx" "b/\351\231\210\344\276\235\346\254\243/20241122\344\275\234\344\270\232.docx" deleted file mode 100644 index 5d44c7c9f05beb6a524343940184ca555a0f33af..0000000000000000000000000000000000000000 Binary files "a/\351\231\210\344\276\235\346\254\243/20241122\344\275\234\344\270\232.docx" and /dev/null differ diff --git "a/\351\231\210\344\276\235\346\254\243/20241125--\351\232\217\346\234\272\346\225\260.md" "b/\351\231\210\344\276\235\346\254\243/20241125--\351\232\217\346\234\272\346\225\260.md" new file mode 100644 index 0000000000000000000000000000000000000000..bea4f48c8615f2f7ef5dda77dda9bf8ba205e037 --- /dev/null +++ "b/\351\231\210\344\276\235\346\254\243/20241125--\351\232\217\346\234\272\346\225\260.md" @@ -0,0 +1,114 @@ +## Action的返回值 +1. 返回常见数据类型,如int、string、Ilist<> +2. IActionResult,返回响应状态码,如200(成功响应)、301(成功响应)、401(找不到任何资源)、404(找不到任何资源)、500(服务端有问题) +3. ActionResult<>,可以同时返回状态码或者常规数据类型 +4. JsonResult、ContentResult,返回响应就是纯粹的数据 +5. POCO:利用返回一个对象,而这个对象在被返回的时候会被序列化(或者反序列化) + 1. 信息--(序列化)--网络(又分三种,网线、光纤、网络设备)-(反序列化)--信息 +``` +public class Product +{ + public int Id { get; set; } + public string Name { get; set; } +} + +public IActionResult GetProduct(int id) +{ + Product product = new Product { Id = id, Name = "Sample Product" }; + return Ok(product); // 返回200状态码和产品对象,对象会被自动序列化为JSON +} +``` + +### 作业 +1. 生成一个随机整数,范围[0,100],注意是否包含 +``` + public string N1(){ + // 初始化随机对象 + var ran = new Random(); + var num1 = ran.Next(0,100); + return num1.ToString(); + } +``` +2. 生成一个随机整数,范围(0,100],注意是否包含 +``` + public string N2(){ + var ran = new Random(); + var num2 = ran.Next(1,100); + return num2.ToString(); + } +``` +3. 生成10个随机整数,范围[5,80],注意是否包含 +``` + public List N3(){ + var ran = new Random(); + + // 定义一个随机数 + int tmp; + // 定义一个数组保存随机数 + var list = new List(); + // 循环取随机数 + for (int i = 0; i < 10; i++) + { + tmp=ran.Next(5,80); + list.Add(tmp); + } + return list; + } + +``` +4. 定义一个字符串,字符串中有100个中文字符,需要从中随机取1个字符串 +``` + public string N4(){ + var ran = new Random(); + var Z1 = "春天来了万物复苏花朵绽放鸟儿歌唱世界充满了生机与色彩让我们拥抱自然感受这温暖的阳光和清新的空气晨光熹微露珠闪烁新的一天带着希望和梦想开始让我们以微笑迎接挑战用行动实现目标向前走未来无限可能"; + int num = ran.Next(0,99); + // int num = ran.Next(1,Z1.Length-1) + var wen = Z1[num]; + return wen.ToString(); + } +``` +5. 定义一个字符串,字符串中有100个中文字符,需要从中随机取5-50个字符,组成新的字符 +``` + public string N5(){ + var ran = new Random(); + var Z2 = "春天来了万物复苏花朵绽放鸟儿歌唱世界充满了生机与色彩让我们拥抱自然感受这温暖的阳光和清新的空气晨光熹微露珠闪烁新的一天带着希望和梦想开始让我们以微笑迎接挑战用行动实现目标向前走未来无限可能"; + // 随机选择5到50个字符 + int num2 = ran.Next(5,51); + string text = ""; + for (int i = 0; i < num2; i++) + { + //index 存储 随机生成字符串 + int index = ran.Next(Z2.Length); + text += Z2[index]; + } + return text; + } +``` +6. 定义2个字符串,第一个字符串中放百家姓,第二个字符串中放中文字符,要求从第一个字符串随机取得一个姓,再从第二个字符串中随机获得1到2个字符组成新字符串,和第一个字符串取得的姓组成一个姓名 +``` + public string N60(){ + var ran = new Random(); + var Z1 = "赵钱孙李周吴郑王冯陈褚卫蒋沈韩杨朱秦尤许何吕施张孔曹严华金魏陶姜戚谢邹喻柏水窦章云苏潘葛奚范彭郎鲁韦昌马苗凤花方俞任袁柳酆鲍史唐费廉岑薛雷贺倪汤滕殷罗毕郝邬安常乐于时傅皮卞齐康伍余元卜顾孟平黄和穆萧尹姚邵湛汪祁毛禹狄米贝明臧计伏成戴谈宋茅庞熊纪舒屈项祝董梁杜阮蓝闵席季麻强贾路娄危江童颜郭梅盛"; + var num1 = ran.Next(0,Z1.Length-1); + var wz = Z1[num1]; + return wz.ToString(); + } + public string N61(){ + var Z2 = "春天来了万物复苏花朵绽放鸟儿歌唱世界充满了生机与色彩让我们拥抱自然感受这温暖的阳光和清新的空气晨光熹微露珠闪烁新的一天带着希望和梦想开始让我们以微笑迎接挑战用行动实现目标向前走未来无限可能"; + var ran = new Random(); + var num2 = ran.Next(1,3); + string text = ""; + for (int i = 0; i < num2; i++) + { + int index = ran.Next(Z2.Length); + text += Z2[index]; + }; + return text; + } + public string N6(){ + var all = N60() + N61(); + return all; + } +``` + +7. 利用以上方法,随机生成100个BlogCreateDto类型(有Title、Author、Content属性)的对象,其中的内容都是随机生成且长度不定,并将其渲染到视图 \ No newline at end of file diff --git "a/\351\231\210\344\276\235\346\254\243/20241127--\350\247\206\345\233\276.md" "b/\351\231\210\344\276\235\346\254\243/20241127--\350\247\206\345\233\276.md" new file mode 100644 index 0000000000000000000000000000000000000000..3900ccb2c810c730ccb493c623b8d23862fb6de7 --- /dev/null +++ "b/\351\231\210\344\276\235\346\254\243/20241127--\350\247\206\345\233\276.md" @@ -0,0 +1,51 @@ +## Razor +Razor 支持 C#,并使用` @ `符号从 HTML 转换为 C#。 Razor 计算 C# 表达式,并将它们呈现在 HTML 输出中。 + +当 @ 符号后跟 Razor 保留关键字时,它会转换为 Razor 特定标记. + +### 隐式 Razor 表达式 +以 @ 开头,后跟 C# 代码。隐式表达式不能包含空格,但 C# **await** 关键字除外。不能包含 C# 泛型,因为括号 (<>) 内的字符会被解释为 HTML 标记。 + +### 显式 Razor 表达式 +由 @ 符号和平衡圆括号组成。 +``` +public class Product +{ + public int Id { get; set; } + public string Name { get; set; } +} + +public IActionResult GetProduct(int id) +{ + Product product = new Product { Id = id, Name = "Sample Product" }; + return Ok(product); // 返回200状态码和产品对象,对象会被自动序列化为JSON +} +``` +``` + [HttpPost] + public IActionResult Create([FromBody]Student student){ + return View(student.Author); + } + public Student Index(int id){ + var blog = new Student{ + Author="巴拉巴拉", + Title="日记", + Content="今天很开心" + }; + return blog; + } + +} +public class Student{ + public string Author{get;set;}=null!; + public string Title{get;set;}=null!; + public string Content{get;set;}=null!; +} +``` +``` + public dynamic Edit(){ + var list = new List(); + list.Add("111"); + return list; + } +``` diff --git "a/\351\231\210\344\276\235\346\254\243/20241129--\346\270\262\346\237\223\345\244\215\346\235\202\350\247\206\345\233\276.md" "b/\351\231\210\344\276\235\346\254\243/20241129--\346\270\262\346\237\223\345\244\215\346\235\202\350\247\206\345\233\276.md" new file mode 100644 index 0000000000000000000000000000000000000000..ff0d0f58633eb3d6ccdec4ab675efa39aac3b32a --- /dev/null +++ "b/\351\231\210\344\276\235\346\254\243/20241129--\346\270\262\346\237\223\345\244\215\346\235\202\350\247\206\345\233\276.md" @@ -0,0 +1,131 @@ +## 定义模型 Models 模拟数据库表 +1. 定义数据库表 +2. 静态构造函数:用于初始化静态属性,只执行一次。 + - 可直接初始化静态属性,例如 public static List Blog = new List{"value1","value2"...}; +3. 集合属性初始化 + - 在静态构造函数中给集合属性 Blogs 添加内容,例如初始化几个 Blog 对象。 + +## HTML视图模型的帮助输入标记 +- 使用 Razor 视图引擎的标记帮助程序,创建一个注册表单。 + - 表单包含 Email 和 Password 输入字段,使用 asp-for +## 注册视图模型 RegisterViewModel +定义一个 RegisterViewModel 类,包含 Email 和 Password 属性,用于表单数据的绑定。 + +# PS:官网:https://learn.microsoft.com 提交到页面和控制器有两种不同方法 +## 作业 + +(Controllers) BlogsController.cs +``` +public class BlogsController : Controller +{ + [HttpPost] + public IActionResult add(Blogs input){ + return Content(JsonSerializer.Serialize (input)); + } + public IActionResult Index(){ + return View(Db.Blogs); + } + public IActionResult delete(){ + return View(); + } + public IActionResult add(){ + return View(); + } + public IActionResult edit(){ + return View(); + } +} + +``` + + +index.html +``` +@model List; + +
+
+ @* *@ + 新增 +
+ + + + + + + + + + @foreach (var blog in @Model) + { + + + + + + + + } + + +
ID标题内容作者操作
@blog.id@blog.Title@blog.Content@blog.Author + @* + *@ + 编辑 + 删除 +
+``` + +add.cshtml +``` +@model Blog.Models.Blogs; +
+
+
+
+ +
+``` + + +(models)blogs.cs +``` +namespace Blog.Models; + +public class Blogs{ + public int id {get;set;} + public string Title {get;set;}=null!; + public string Content {get;set;}=null!; + public string Author {get;set;}=null!; +} +``` + +(models)Db.cs +``` +namespace Blog.Models; +// 是数据库 +public static class Db{ + // 定义一个数组 + public static List Blogs {get;set;} + + // 保存 + static Db(){ + // 初始化 + Blogs = new List(); + for (int i = 0; i < 12; i++) + { + var data = new Blogs { + id=i, + Title = "黑恶黑和黑", + Content = "123332", + Author = "巴拉巴拉巴拉" + }; + // 将数据添加到数组中 + Blogs.Add(data); + } + + } +} +``` +