diff --git "a/\351\231\210\345\207\257\347\220\233/1124\344\275\234\344\270\232/20241119\347\254\224\350\256\260.md" "b/\351\231\210\345\207\257\347\220\233/1124\344\275\234\344\270\232/20241119\347\254\224\350\256\260.md" new file mode 100644 index 0000000000000000000000000000000000000000..5a4d4addf77d7cb9584db6291764723dc0962b86 --- /dev/null +++ "b/\351\231\210\345\207\257\347\220\233/1124\344\275\234\344\270\232/20241119\347\254\224\350\256\260.md" @@ -0,0 +1,11 @@ +# 控制器 +名字:控制器名+Controller.cs 文件内的基本组成: + +using Microsoft.AspNetCore.Mvc +namespace MVC项目名.Controlers; +public class 类名 : Controller +{ + public 返回值 方法名(参数){ + return 值; + } +} \ No newline at end of file diff --git "a/\351\231\210\345\207\257\347\220\233/1124\344\275\234\344\270\232/20241121\347\254\224\350\256\260.md" "b/\351\231\210\345\207\257\347\220\233/1124\344\275\234\344\270\232/20241121\347\254\224\350\256\260.md" new file mode 100644 index 0000000000000000000000000000000000000000..aaf30593532942ca5a0c4f864c4750e16ffae95e --- /dev/null +++ "b/\351\231\210\345\207\257\347\220\233/1124\344\275\234\344\270\232/20241121\347\254\224\350\256\260.md" @@ -0,0 +1,26 @@ +## POST 请求的处理及示例 + +### `POST` 请求的基本概念 + +`POST` 请求用于向服务器发送数据,通常用来提交表单信息或者发送 JSON 数据。与 `GET` 请求不同,`POST` 请求的数据不会暴露在 URL 中,而是通过请求体发送。 + +### 示例:接收并显示昵称 + +以下是一个简单的 ASP.NET Core 控制器示例,它接收客户端通过 `POST` 请求发送的 JSON 数据,获取其中的昵称并在网页上显示。 + +### POST 请求头 + +##### 模拟POST请求 + +发送的 `POST` 请求 + +请求体: + +```json +{ + "nickname": "测试名字" +} +``` + +- **请求头**:`Content-Type: application/json` 表示请求体中的数据是 JSON 格式。 +- **请求体**:包含了需要传输的数据,这里是 `nickname` 字段和它的值 `"测试名字"`。 diff --git "a/\351\231\210\345\207\257\347\220\233/1124\344\275\234\344\270\232/20241122\347\254\224\350\256\260.md" "b/\351\231\210\345\207\257\347\220\233/1124\344\275\234\344\270\232/20241122\347\254\224\350\256\260.md" new file mode 100644 index 0000000000000000000000000000000000000000..39aeba3ff60ba72cf6a616adb8374af21e3b8d21 --- /dev/null +++ "b/\351\231\210\345\207\257\347\220\233/1124\344\275\234\344\270\232/20241122\347\254\224\350\256\260.md" @@ -0,0 +1,78 @@ + +##### 返回结果 + +```http +HTTP/1.1 200 OK +Connection: close +Content-Type: text/html; charset=utf-8 +Date: Mon, 18 Nov 2024 14:22:21 GMT +Server: Kestrel +Transfer-Encoding: chunked + +

接收到的昵称是:测试名字

+``` + +### 控制器部分 + +#### 模型类 (`NicknameModel`) + +```csharp +public class NicknameModel +{ + public required string Nickname { get; set; } +} +``` + +- **作用**:模型类定义了一个 `Nickname` 字段,用于接收客户端发送的昵称。通过 `required` 关键字,确保客户端必须提供 `nickname` 数据,否则会出现错误。 + +#### 控制器类 (`PostController`) + +```csharp +public class PostController : Controller +{ + [HttpPost] + public IActionResult Index([FromBody] NicknameModel model) + { + // 从请求中获取昵称数据 + string nickname = model.Nickname; + + // 将昵称存储到 ViewData 中,以便在视图中使用 + ViewData["Nickname"] = nickname; + + // 返回视图 + return View(); + } +} +``` + +- **作用**:控制器接收 `POST` 请求中的数据,并将昵称存储到 `ViewData` 中,最后返回一个视图来显示该昵称。 + + `[FromBody]` 表示框架从请求体中获取数据,并将其自动转换为方法参数所需的类型。在此示例中,它会将请求体中的 JSON 数据转换为 `NicknameModel` 对象。 + +#### 视图部分 (`Index.cshtml`) + +```html +@using Microsoft.AspNetCore.Mvc.ViewFeatures +@{ + ViewData["Title"] = "昵称显示"; +} +

接收到的昵称是:@Html.Raw(ViewData["Nickname"])

+@* @Html.Raw() 会告诉 Razor 引擎将 ViewData["Nickname"] 渲染为原始 HTML 内容,不进行转义。 *@ +``` + +- **作用**:视图部分用来显示传递过来的昵称。通过 `@ViewData["Nickname"]`,在页面中显示控制器传递过来的昵称数据。 + + 且通过 `@Html.Raw(ViewData["Nickname"])`,在页面中显示控制器传递过来的昵称数据。`@Html.Raw()` 会防止 HTML 转义,直接渲染原始内容。 + +### 代码流向 + +1. **客户端**:通过 `POST` 请求发送 JSON 数据到服务器。 +2. **控制器**:接收并解析 `POST` 请求中的数据。 +3. **视图**:展示控制器传递的数据。 + +### 总结 + +- **`POST` 请求**:用于向服务器提交数据。常用于表单提交或 JSON 数据交互。 +- **模型类**:用于接收客户端发送的 JSON 数据。 +- **控制器类**:处理 `POST` 请求,解析数据并传递给视图。 +- **视图部分**:显示控制器传递的数据,最终在网页上展现给用户。 \ No newline at end of file diff --git "a/\351\231\210\345\207\257\347\220\233/1124\344\275\234\344\270\232/Linux\344\275\234\344\270\232.md" "b/\351\231\210\345\207\257\347\220\233/1124\344\275\234\344\270\232/Linux\344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..aea879c4a5ba13b220f77e4aaaee810f12e7de80 --- /dev/null +++ "b/\351\231\210\345\207\257\347\220\233/1124\344\275\234\344\270\232/Linux\344\275\234\344\270\232.md" @@ -0,0 +1,145 @@ +# 搭建十个域名 +1. +![eybvlfndlkw](https://20050206kc.oss-cn-guangzhou.aliyuncs.com/eybvlfndlkw.gif) + +2. +![ghfdiwknkgv](https://20050206kc.oss-cn-guangzhou.aliyuncs.com/ghfdiwknkgv.gif) + +3. +![jfksajkf](https://20050206kc.oss-cn-guangzhou.aliyuncs.com/jfksajkf.gif) + +4. +![jkwjknkdsl](https://20050206kc.oss-cn-guangzhou.aliyuncs.com/jkwjknkdsl.gif) + +5. +![kvsankvnksavnklsd](https://20050206kc.oss-cn-guangzhou.aliyuncs.com/kvsankvnksavnklsd.gif) + +6. +![ogjsadojk](https://20050206kc.oss-cn-guangzhou.aliyuncs.com/ogjsadojk.gif) + +7. +![ojkhodsjp](https://20050206kc.oss-cn-guangzhou.aliyuncs.com/ojkhodsjp.gif) + +8. +![safasfsafasfsaf](https://20050206kc.oss-cn-guangzhou.aliyuncs.com/safasfsafasfsaf.gif) + +9. +![sanfknovamsov](https://20050206kc.oss-cn-guangzhou.aliyuncs.com/sanfknovamsov.gif) + +10. +![xinwekelf](https://20050206kc.oss-cn-guangzhou.aliyuncs.com/xinwekelf.gif) + +# 部分命令任务 +任务:更新软件源,命令:apt update,并了解这一步的实际用处和意义 +1.apt update -y apt upgrade -y +![hehesafdafxa](https://20050206kc.oss-cn-guangzhou.aliyuncs.com/hehesafdafxa.gif) +任务:更新软件和补丁,命令:apt upgrade -y,并了解这一步的实际用处意义 +2. +![hihihihihi](https://20050206kc.oss-cn-guangzhou.aliyuncs.com/hihihihihi.gif) + +任务:熟悉并完成以下练习 +3. +![teadaf](https://20050206kc.oss-cn-guangzhou.aliyuncs.com/teadaf.gif) + +任务:尝试使用以下几种方式分别登录服务器,说明它们分别的注意事项,并说明它们之间的区别 + +ssh客户端 +tabby应用 +xShell +putty + +### SSH客户端 + +**注意事项:** +1. 确保拥有服务器的SSH IP地址和端口号。 +2. 输入正确的用户名和密码。 +3. 根据个人喜好设置字符集、字体样式及大小、窗口保存的内容行数等。 +4. 设置自动保持连接,以免服务器长时间没有收到数据包而自动断开SSH连接。 + +### Tabby应用 + +**注意事项:** +1. Tabby支持多平台,确保下载适合自己操作系统的版本。 +2. Tabby自带SFTP功能,可以用于文件传输。 +3. 在设置SSH连接时,需要填写终端名称、IP地址、端口号、账号密码等信息。 +4. Tabby支持插件,可以根据需要安装和配置插件以增强功能。 + +### XShell + +**注意事项:** +1. XShell主要支持Windows系统。 +2. 设置不间断连接,以防止服务器因长时间无操作而断开连接。 +3. 关闭响铃和X11,特别是在免费版本中,以避免提示购买Xmanager。 +4. 可以调整视图设置和主题设置,以适应个人偏好。 + +### PuTTY + +**注意事项:** +1. PuTTY支持WIN和MAC系统,确保下载适合自己操作系统的版本。 +2. 修改字符集解决中文乱码问题。 +3. 修改字体样式及大小,以及窗口保存的内容行数。 +4. 设置自动保持连接,以维持SSH连接的稳定性。 + +### 区别 + +1. **平台支持:** + - **PuTTY**:支持Windows和Mac系统。 + - **Tabby**:支持Windows、MacOS(包括Intel芯片和M1芯片)、Linux。 + - **XShell**:主要支持Windows系统。 + +2. **界面和体验:** + - **PuTTY**:界面较为简单,功能基本。 + - **Tabby**:提供炫酷的终端页面,简单易用,支持插件。 + - **XShell**:提供多标签模式,方便同时管理多个服务器。 + +3. **功能丰富性:** + - **PuTTY**:功能相对较少,适合快速连接。 + - **Tabby**:功能丰富,自带SFTP功能,支持多平台。 + - **XShell**:功能丰富,提供多标签和文件传输等高级功能。 + +4. **文件传输:** + - **PuTTY**:需要使用PuTTYgen将pem文件转换成ppk格式进行文件传输。 + - **Tabby**:自带SFTP功能,方便文件传输。 + - **XShell**:如果安装了xftp,可以直接调用xftp进行文件传输。 + +5. **开源性:** + - **PuTTY**:免费开源。 + - **Tabby**:开源。 + - **XShell**:有商业版本,提供更多高级功能和技术支持。 + +1. +![sdikofnaksnv](https://20050206kc.oss-cn-guangzhou.aliyuncs.com/sdikofnaksnv.gif) + +2. +![vnodnvodnvodnsovn](https://20050206kc.oss-cn-guangzhou.aliyuncs.com/vnodnvodnvodnsovn.gif) + +3. +![three](https://20050206kc.oss-cn-guangzhou.aliyuncs.com/three.gif) + +4. +![fourth](https://20050206kc.oss-cn-guangzhou.aliyuncs.com/fourth.gif) + +5. +![fifth](https://20050206kc.oss-cn-guangzhou.aliyuncs.com/fifth.gif) + +6. +![sixth](https://20050206kc.oss-cn-guangzhou.aliyuncs.com/sixth.gif) + +7-9 +![seven eight ninth](https://20050206kc.oss-cn-guangzhou.aliyuncs.com/seven eight ninth.gif) + +10-12. +![101112](https://20050206kc.oss-cn-guangzhou.aliyuncs.com/101112.gif) + + +13-17 +![20241124192406](https://20050206kc.oss-cn-guangzhou.aliyuncs.com/20241124192406.png) + +18-23 +![20241124193007](https://20050206kc.oss-cn-guangzhou.aliyuncs.com/20241124193007.png) + +# 安装sdk环境 +![20241124193153](https://20050206kc.oss-cn-guangzhou.aliyuncs.com/20241124193153.png) + +# 任务:使用nginx反向代理,部署简单的MVC项目 +![20241124193453](https://20050206kc.oss-cn-guangzhou.aliyuncs.com/20241124193453.png) \ No newline at end of file diff --git "a/\351\231\210\345\207\257\347\220\233/1124\344\275\234\344\270\232/MVC\344\275\234\344\270\232.md" "b/\351\231\210\345\207\257\347\220\233/1124\344\275\234\344\270\232/MVC\344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..f1fd43aabbe50c628bf9ce6aa25188df126ca199 --- /dev/null +++ "b/\351\231\210\345\207\257\347\220\233/1124\344\275\234\344\270\232/MVC\344\275\234\344\270\232.md" @@ -0,0 +1,80 @@ +1.创建一个控制台项目,没有任何选项,体会项目名称和什么有关系 +![20241124195211](https://20050206kc.oss-cn-guangzhou.aliyuncs.com/20241124195211.png) + + +2.创建一个控制项目,项目名称Blog + +![20241124195313](https://20050206kc.oss-cn-guangzhou.aliyuncs.com/20241124195313.png) + +3.创建一个控制台项目,输出到Blog目录 + +![20241124195432](https://20050206kc.oss-cn-guangzhou.aliyuncs.com/20241124195432.png) + +4.创建一个MVC项目,指定项目名称 + +![20241124195539](https://20050206kc.oss-cn-guangzhou.aliyuncs.com/20241124195539.png) + +5.创建一个MVC项目,指定输出目录 + +![20241124195745](https://20050206kc.oss-cn-guangzhou.aliyuncs.com/20241124195745.png) + +6.创建一个带解决方案,其下有一个MVC项目,3个类库项目的“综合项目” + +![20241124200102](https://20050206kc.oss-cn-guangzhou.aliyuncs.com/20241124200102.png) + +7.创建一个项目,在默认控制器(Home)下,新增一个Action方法,名为Ok,同时为其创建对应视图以显示这个视图 + +![20241124204511](https://20050206kc.oss-cn-guangzhou.aliyuncs.com/20241124204511.png) + +8.创建一个项目,创建一个新的控制器,名为Blogs,新的控制器拥有一个名为Index的Action,该方法返回一个视图,视图显示“神级预判” + +![20241124205741](https://20050206kc.oss-cn-guangzhou.aliyuncs.com/20241124205741.png) + +9.给第8题的新控制,添加一个新的Action,名为Music,不接受任何参数,并返回对应的视图,视图显示“顶级打野” + +![20241124205753](https://20050206kc.oss-cn-guangzhou.aliyuncs.com/20241124205753.png) + +10.给第8题的新控制器,新增一个Action,名为List,不接受任何参数,并返回对应视图,视图显示一个经典CRUD界面 + + public IActionResult List() + { + return View(); + } + +11.新增一个控制器,名为Products,该控制器具有一个名为Edit的Action,这个Action接受一个int类型的参数id,显示这个id + +```html +public class ProductsController : Controller +{ + public IActionResult Edit(int id) + { + ViewBag.Id = id; + return View(); + } +} +创建对应视图Views/Products/Edit.cshtml + +

Edit Product

+

编辑的产品ID:@ViewBag.Id

+``` + +12.在11题的新控制器中,新增一个名为Create的Action,该Action接受一个类型为Students(有姓名、年龄、体长属性)的参数,并展示该参数的姓名属性 + +```html +public class ProductsController : Controller{ + public IActionResult Create(){ + var pro=new ProductStudent{ + Name="tony", + Age="24", + weight="359t" + }; + return View(pro); +} + } + public class ProductStudent{ + public string Name{get;set;}=null!; + public string Age{get;set;}=null!; + public string Tall{get;set;}=null!; + + } +``` \ No newline at end of file diff --git "a/\351\231\210\345\207\257\347\220\233/1124\344\275\234\344\270\232/\345\237\272\347\241\200\350\203\275\345\212\233.cs" "b/\351\231\210\345\207\257\347\220\233/1124\344\275\234\344\270\232/\345\237\272\347\241\200\350\203\275\345\212\233.cs" new file mode 100644 index 0000000000000000000000000000000000000000..a1c2656f9809c09a3d59dec91308fd3e6fbd2cb8 --- /dev/null +++ "b/\351\231\210\345\207\257\347\220\233/1124\344\275\234\344\270\232/\345\237\272\347\241\200\350\203\275\345\212\233.cs" @@ -0,0 +1,99 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Web.Mvc; + +namespace MVCRandomExample.Controllers +{ + public class RandomGeneratorController : Controller + { + // 1. 生成一个随机整数,范围[0,100],注意是否包含 + public ActionResult GenerateRandomIntegerInclusive() + { + Random random = new Random(); + int randomNumber = random.Next(0, 101); // [0, 100] inclusive + return Content($"Random Number (inclusive): {randomNumber}"); + } + + // 2. 生成一个随机整数,范围(0,100],注意是否包含 + public ActionResult GenerateRandomIntegerExclusive() + { + Random random = new Random(); + int randomNumber = random.Next(1, 101); // (0, 100] exclusive + return Content($"Random Number (exclusive): {randomNumber}"); + } + + // 3. 生成10个随机整数,范围[5,80],注意是否包含 + public ActionResult GenerateMultipleRandomIntegers() + { + Random random = new Random(); + List randomNumbers = new List(); + for (int i = 0; i < 10; i++) + { + int randomNumber = random.Next(5, 81); // [5, 80] inclusive + randomNumbers.Add(randomNumber); + } + return Content($"Random Numbers: {string.Join(", ", randomNumbers)}"); + } + + // 4. 定义一个字符串,字符串中有100个中文字符,需要从中随机取1个字符串 + public ActionResult RandomSingleCharacterFromString() + { + string characters = "你好世界这是一百个中文字符的字符串"; + Random random = new Random(); + char randomChar = characters[random.Next(characters.Length)]; + return Content($"Random Character: {randomChar}"); + } + + // 5. 定义一个字符串,字符串中有100个中文字符,需要从中随机取5-50个字符,组成新的字符 + public ActionResult RandomSubstringFromString() + { + string characters = "你好世界这是一百个中文字符的字符串"; + Random random = new Random(); + int length = random.Next(5, 51); // 5 to 50 inclusive + int startIndex = random.Next(characters.Length - length + 1); + string substring = characters.Substring(startIndex, length); + return Content($"Random Substring: {substring}"); + } + + // 6. 定义2个字符串,第一个字符串中放百家姓,第二个字符串中放中文字符,要求从第一个字符串随机取得一个姓,再从第二个字符串中随机获得1到2个字符组成新字符串,和第一个字符串取得的姓组成一个姓名 + public ActionResult GenerateRandomName() + { + string surnames = "赵钱孙李周吴郑王冯陈褚卫蒋沈韩杨朱秦尤许何吕施张孔曹严华金魏陶姜戚谢邹喻柏水窦章云苏潘葛奚范彭郎鲁韦昌马苗凤花方俞任袁柳酆鲍史唐费廉岑薛雷贺倪汤滕殷罗毕郝邬安常乐于时傅皮卞齐康伍余元卜顾孟平黄和穆萧尹姚邵湛汪祁毛禹狄米贝明臧计伏成戴谈宋茅庞熊纪舒屈项祝董梁杜阮蓝闵席季麻强贾路娄危江童颜郭梅盛林刁钟徐邱骆高夏蔡田樊胡凌霍虞万支柯昝管卢莫经房裘缪干解应宗丁宣贲邓郁单杭洪包诸左石崔吉钮龚程嵇邢滑裴陆荣翁荀羊于惠甄曲家封芮羿储靳汲邴糜松井段富巫乌焦巴弓牧隗山谷车侯宓蓬全郗班仰秋仲伊宫宁仇栾暴甘钭厉戎祖武符刘景詹束龙叶幸司韶郜黎蓟薄印宿白怀蒲台从鄂索咸籍赖卓蔺屠蒙池乔阴jinsuaziwuyuanguangning"; + string characters = "你好世界这是一百个中文字符的字符串"; + Random random = new Random(); + char surname = surnames[random.Next(surnames.Length)]; + int nameLength = random.Next(1, 3); // 1 or 2 characters + string namePart = string.Empty; + for (int i = 0; i < nameLength; i++) + { + namePart += characters[random.Next(characters.Length)]; + } + string fullName = surname + namePart; + return Content($"Random Name: {fullName}"); + } + + // 7. 利用以上方法,随机生成100个BlogCreateDto类型(有Title、Author、Content属性)的对象,其中的内容都是随机生成且长度不定,并将其渲染到视图 + public ActionResult GenerateBlogPosts() + { + List blogPosts = new List(); + Random random = new Random(); + for (int i = 0; i < 100; i++) + { + int titleLength = random.Next(5, 20); // Random title length between 5 and 20 + int contentLength = random.Next(50, 200); // Random content length between 50 and 200 + string title = new string((char)random.Next('A', 'Z'), titleLength); + string content = new string((char)random.Next('a', 'z'), contentLength); + blogPosts.Add(new BlogCreateDto { Title = title, Author = "Author" + i, Content = content }); + } + return View(blogPosts); + } + } + + public class BlogCreateDto + { + public string Title { get; set; } + public string Author { get; set; } + public string Content { get; set; } + } +} diff --git "a/\351\231\210\345\207\257\347\220\233/1124\344\275\234\344\270\232/\346\216\247\345\210\266\345\231\250\344\274\240\345\217\202.md" "b/\351\231\210\345\207\257\347\220\233/1124\344\275\234\344\270\232/\346\216\247\345\210\266\345\231\250\344\274\240\345\217\202.md" new file mode 100644 index 0000000000000000000000000000000000000000..1c1ffcaa279392965755fb44aa2b86897658a27e --- /dev/null +++ "b/\351\231\210\345\207\257\347\220\233/1124\344\275\234\344\270\232/\346\216\247\345\210\266\345\231\250\344\274\240\345\217\202.md" @@ -0,0 +1,184 @@ +# 专项练习-控制器传参 +1.简单参数传递 在一个叫Blog控制器中,定义一个叫Index的Action,并且传递一个int类型的值,id为变量名 +```html +// BlogController.cs +using Microsoft.AspNetCore.Mvc; + +namespace MyApp.Controllers +{ + public class BlogController : Controller + { + public IActionResult Index(int id) + { + return Content($"ID: {id}"); + } + } +} + +``` +2.简单参数传递 在一个叫Blog控制器中,定义一个叫Index_2的Action,并且传递一个string类型的值,id为变量名 +```html +// BlogController.cs +using Microsoft.AspNetCore.Mvc; + +namespace MyApp.Controllers +{ + public class BlogController : Controller + { + public IActionResult Index_2(string id) + { + return Content($"ID: {id}"); + } + } +} + +``` +3.简单参数传递 在一个叫Blog控制器中,定义一个叫Index_3的Action,并且传递一个string类型的值,name为变量名 +```html +// BlogController.cs +using Microsoft.AspNetCore.Mvc; + +namespace MyApp.Controllers +{ + public class BlogController : Controller + { + public IActionResult Index_3(string name) + { + return Content($"Name: {name}"); + } + } +} + +``` +4.复杂参数传递 在一个叫Blog的控制器中,定义一个名为Create的Action,并且传递一个BlogCreateDto类型的值,blogCreateDto为变量名 +```html +// BlogController.cs +using Microsoft.AspNetCore.Mvc; +using MyApp.Models; + +namespace MyApp.Controllers +{ + public class BlogController : Controller + { + [HttpPost] + public IActionResult Create([FromBody] BlogCreateDto blogCreateDto) + { + return Content($"Title: {blogCreateDto.Title}, Author: {blogCreateDto.Author}, Content: {blogCreateDto.Content}"); + } + } +} + +// BlogCreateDto.cs +namespace MyApp.Models +{ + public class BlogCreateDto + { + public string Title { get; set; } + public string Author { get; set; } + public string Content { get; set; } + } +} + +``` +5.PS BlogCreateDto类型具有Title、Author、Content自动属性 +```html +// BlogCreateDto.cs (already defined above) +namespace MyApp.Models +{ + public class BlogCreateDto + { + public string Title { get; set; } + public string Author { get; set; } + public string Content { get; set; } + } +} + +``` +6.复杂参数传递 在一个叫Blog的控制器中,定义一个名为Create_1的Action,并且传递一个Products类型的值,productCreateDto为变量名 +```html +// BlogController.cs +using Microsoft.AspNetCore.Mvc; +using MyApp.Models; + +namespace MyApp.Controllers +{ + public class BlogController : Controller + { + [HttpPost] + public IActionResult Create_1([FromBody] Products productCreateDto) + { + return Content($"Name: {productCreateDto.Name}, Price: {productCreateDto.Price}, Stock: {productCreateDto.Stock}"); + } + } +} + +// Products.cs +namespace MyApp.Models +{ + public class Products + { + public string Name { get; set; } + public double Price { get; set; } + public int Stock { get; set; } + } +} + +``` +7.PS Products类型具有Name、Price、Stock自动属性 +```html +// Products.cs (already defined above) +namespace MyApp.Models +{ + public class Products + { + public string Name { get; set; } + public double Price { get; set; } + public int Stock { get; set; } + } +} + +``` +8.复杂参数传递 在一个叫Blog的控制器中,定义一个名为Create_2的Action,并且传递一个Students类型的值,studentCreateDto为变量名 +```html +// BlogController.cs +using Microsoft.AspNetCore.Mvc; +using MyApp.Models; + +namespace MyApp.Controllers +{ + public class BlogController : Controller + { + [HttpPost] + public IActionResult Create_2([FromBody] Students studentCreateDto) + { + return Content($"StudentName: {studentCreateDto.StudentName}, Sex: {studentCreateDto.Sex}, Age: {studentCreateDto.Age}"); + } + } +} + +// Students.cs +namespace MyApp.Models +{ + public class Students + { + public string StudentName { get; set; } + public string Sex { get; set; } + public int Age { get; set; } + } +} + +``` +9.PS Students类型具有StudentName、Sex、Age自动属性 +```html +// Students.cs (already defined above) +namespace MyApp.Models +{ + public class Students + { + public string StudentName { get; set; } + public string Sex { get; set; } + public int Age { get; set; } + } +} + +``` \ No newline at end of file diff --git "a/\351\231\210\345\207\257\347\220\233/1124\344\275\234\344\270\232/\346\270\262\346\237\223\347\273\203\344\271\240.md" "b/\351\231\210\345\207\257\347\220\233/1124\344\275\234\344\270\232/\346\270\262\346\237\223\347\273\203\344\271\240.md" new file mode 100644 index 0000000000000000000000000000000000000000..2fa36733e46f687c828bc4653b5afa5f65ece7b4 --- /dev/null +++ "b/\351\231\210\345\207\257\347\220\233/1124\344\275\234\344\270\232/\346\270\262\346\237\223\347\273\203\344\271\240.md" @@ -0,0 +1,137 @@ + +### 1. 渲染简单数据到页面 + +#### 模型 (Models/SimpleData.cs) +```csharp +public class SimpleData +{ + public string Message { get; set; } +} +``` + +#### 控制器 (Controllers/HomeController.cs) +```csharp +using System.Web.Mvc; +using YourNamespace.Models; + +public class HomeController : Controller +{ + public ActionResult SimpleData() + { + var model = new SimpleData { Message = "Hello, World!" }; + return View(model); + } +} +``` + +#### 视图 (Views/Home/SimpleData.cshtml) +```html +@model YourNamespace.Models.SimpleData + +

@Model.Message

+``` + +### 2. 渲染复杂数据到页面 + +#### 模型 (Models/ComplexData.cs) +```csharp +public class ComplexData +{ + public string Name { get; set; } + public int Age { get; set; } + public List Hobbies { get; set; } +} +``` + +#### 控制器 (Controllers/HomeController.cs) +```csharp +using System.Collections.Generic; +using System.Web.Mvc; +using YourNamespace.Models; + +public class HomeController : Controller +{ + public ActionResult ComplexData() + { + var model = new ComplexData + { + Name = "John Doe", + Age = 30, + Hobbies = new List { "Reading", "Traveling", "Swimming" } + }; + return View(model); + } +} +``` + +#### 视图 (Views/Home/ComplexData.cshtml) +```html +@model YourNamespace.Models.ComplexData + +

Name: @Model.Name

+

Age: @Model.Age

+

Hobbies:

+
    + @foreach (var hobby in Model.Hobbies) + { +
  • @hobby
  • + } +
+``` + +### 3. 渲染集合数据到页面 + +#### 模型 (Models/Item.cs) +```csharp +public class Item +{ + public string Name { get; set; } + public double Price { get; set; } +} +``` + +#### 控制器 (Controllers/HomeController.cs) +```csharp +using System.Collections.Generic; +using System.Web.Mvc; +using YourNamespace.Models; + +public class HomeController : Controller +{ + public ActionResult CollectionData() + { + var items = new List + { + new Item { Name = "Apple", Price = 0.99 }, + new Item { Name = "Banana", Price = 0.59 }, + new Item { Name = "Cherry", Price = 2.99 } + }; + return View(items); + } +} +``` + +#### 视图 (Views/Home/CollectionData.cshtml) +```html +@model IEnumerable + +

Items

+ + + + + + + + + @foreach (var item in Model) + { + + + + + } + +
NamePrice
@item.Name$@item.Price
+``` +