diff --git "a/\350\214\203\351\270\277\351\233\257/20241119-/1.txt" "b/\350\214\203\351\270\277\351\233\257/20241119-/1.txt" deleted file mode 100644 index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0000000000000000000000000000000000000000 diff --git "a/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/20241119-\346\216\247\345\210\266\345\231\250.md" "b/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/20241119-\346\216\247\345\210\266\345\231\250.md" new file mode 100644 index 0000000000000000000000000000000000000000..aaef55a0c76183c27501c1a73727151bf46eeae2 --- /dev/null +++ "b/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/20241119-\346\216\247\345\210\266\345\231\250.md" @@ -0,0 +1,246 @@ +# ASP.NET Core MVC 控制器 + +## 1. 控制器概述 +- 控制器是ASP.NET Core MVC中的一个核心组件,负责处理用户请求并返回响应。 +- 控制器通常会调用模型,准备数据,然后选择视图进行渲染。 + +## 2. 控制器的创建 +- 使用`Controller`基类。 +- 控制器类命名通常以`Controller`结尾,例如`HomeController`。 + +```csharp +public class HomeController : Controller +{ + public IActionResult Index() + { + return View(); + } +} +``` + +## 3. 路由 +- ASP.NET Core使用属性路由或基于约定的路由。 +- 路由可以在`Startup.cs`文件中配置。 + +```csharp +app.UseEndpoints(endpoints => +{ + endpoints.MapControllerRoute( + name: "default", + pattern: "{controller=Home}/{action=Index}/{id?}"); +}); +``` + +## 4. 动作方法 +- 控制器中的每个公共方法称为动作方法(Action Method)。 +- 动作方法可以返回不同类型的结果,如`IActionResult`,`JSONResult`等。 + +### 常用返回类型: +- **ViewResult**: 返回视图。 +- **RedirectResult**: 重定向到另一个URL。 +- **JsonResult**: 返回JSON数据。 +- **ContentResult**: 返回纯文本内容。 + +```csharp +public IActionResult About() +{ + return View(); +} +``` + +## 5. 参数绑定 +- 参数可以通过路由、查询字符串或表单数据绑定到动作方法。 + +```csharp +public IActionResult Details(int id) +{ + // 使用id参数 +} +``` + +### 6. 特性(Attributes) +- 可以使用特性装饰控制器和动作方法,控制访问和处理。 + +#### 常见特性: +- `[HttpGet]`、`[HttpPost]`:指定HTTP方法。 +- `[Route("custom-route")]`:自定义路由。 +- `[Authorize]`:控制访问权限。 + +```csharp +[HttpPost] +public IActionResult Create([FromBody] Item item) +{ + // 创建逻辑 +} +``` + +## 7. 依赖注入 +- 控制器可以通过构造函数注入服务。 + +```csharp +public class HomeController : Controller +{ + private readonly IMyService _myService; + + public HomeController(IMyService myService) + { + _myService = myService; + } +} +``` + +## 8. 视图选择 +- 可以在动作方法中手动选择视图。 +- 通过返回`View("ViewName")`指定视图名称。 + +```csharp +public IActionResult CustomView() +{ + return View("MyView"); +} +``` + +## 9. 异常处理 +- 可以在控制器中使用`try-catch`块捕获异常。 +- 使用中间件处理全局异常。 + +## 10. 测试控制器 +- 控制器可以通过模拟HTTP请求进行单元测试。 +- 使用测试框架如xUnit或NUnit。 +# 1.任务:尝试使用以下几种方式分别登录服务器,说明它们分别的注意事项,并说明它们之间的区别 + +## ssh客户端 +SSH客户端通常指的是命令行工具,它的优点在于轻量和灵活,但对于不熟悉命令行的用户来说,使用门槛稍高。 +## tabby应用 +Tabby提供了图形界面,相比纯命令行的SSH客户端,易于使用,并具备会话管理和分屏等功能,更适合不熟悉命令行的用户。 +## xShell +xShell具有丰富的功能,如端口转发、脚本支持、标签页管理、可自定义的快捷键,针对专业用户设计,但也可能相对复杂。 +## putty +PuTTY界面简洁,设置简单,适合初学者,但相比于xShell等可能缺少一些高级功能。 +# 2.任务:更新软件源,命令:apt update,并了解这一步的实际用处和意义 +![](./imgs/1.png) +# 3.任务:更新软件和补丁,命令:apt upgrade -y,并了解这一步的实际用处意义 +![](./imgs/2.png) +# 4.任务:熟悉并完成以下练习 + 1.查看当前目录下的文件和文件夹 + 命令:ls + ![](./imgs/5-1.png) + 2.查看当前路径 + + 命令:pwd +![](./imgs/5-3.png) + 3.创建一个新的文件夹 + + 命令:mkdir [文件夹名] +![](./imgs/5-4.png) + 4.删除一个文件夹 + + 命令:rmdir [文件夹名](注意:只能删除空文件夹) +![](./imgs/5-5.png) + 5.移动或重命名文件/文件夹 + + 命令:mv [原路径] [新路径] +![](./imgs/5-2.png) + 6.复制文件 + + 命令:cp [源文件] [目标路径] +![](./imgs/5-6.png) + 7.删除文件 + + 命令:rm [文件名] + ![](./imgs/5-7.png) + 8.查看文件内容 + + 命令:cat [文件名] +![](./imgs/5-8.png) + 9.分页查看文件内容 + + 命令:less [文件名] + ![](./imgs/5-9.png) + 10.查找文件 + + 命令:find / -name [文件名] + ![](./imgs/5-10.png) + 11.查看文件权限 + + 命令:ls -l [文件或目录名] + ![](./imgs/5-11.png) + 12.改变文件权限 + + 命令:chmod [权限] [文件或目录名] + ![](./imgs/5-12.png) + 13.改变文件所有者 + + 命令:chown [新所有者] [文件或目录名] + ![](./imgs/5-13.png) + 14.查看当前登录用户 + + 命令:whoami + ![](./imgs/5-14.png) + 15.查看系统运行时间和平均负载 + + 命令:uptime + ![](./imgs/5-15.png) + 16.查看磁盘使用情况 + + 命令:df -h + ![](./imgs/5-16.png) + 17.查看当前路径下的隐藏文件 + + 命令:ls -a + ![](./imgs/5-17.png) + 18.创建一个空文件 + + 命令:touch [文件名] + ![](./imgs/5-18.png) + 19.查看当前系统的内核版本 + + 命令:uname -r + ![](./imgs/5-19.png) + 20.查看网络连接状态 + + 命令:ifconfig 或 ip addr + ![](./imgs/5-20.png) + 21.安装一个软件包 + + 命令:sudo apt-get install [软件包名] + ![](./imgs/5-21.png) + 22.卸载一个软件包 + + 命令:sudo apt-get remove [软件包名] + ![](./imgs/5-22.png) + 23.更新软件包列表 + + 命令:sudo apt-get update + ![](./imgs/5-23.png) +# 5.任务:在服务器上安装SDK环境 +![](./imgs/3.png) +# 6.任务:使用nginx部署10个静态网站,要求不同域名,不同内容 + +# 7.任务:使用nginx反向代理,部署简单的MVC项目 + +# MVC 练习 +1.创建一个控制台项目,没有任何选项,体会项目名称和什么有关系 +![](./imgs/8-1.png) +2.创建一个控制项目,项目名称Blog +![](./imgs/8-2.png) +3.创建一个控制台项目,输出到Blog目录 +![](./imgs/8-3.png) +4.创建一个MVC项目,指定项目名称 +![](./imgs/8-4.png) +5.创建一个MVC项目,指定输出目录 +![](./imgs/8-5.png) +6.创建一个带解决方案,其下有一个MVC项目,3个类库项目的“综合项目” +![](./imgs/8-6.png) +7.创建一个项目,在默认控制器(Home)下,新增一个Action方法,名为Ok,同时为其创建对应视图以显示这个视图 +![](./imgs/8-7.png) +8.创建一个项目,创建一个新的控制器,名为Blogs,新的控制器拥有一个名为Index的Action,该方法返回一个视图,视图显示“神级预判” +![](./imgs/8-8.png) +9.给第8题的新控制,添加一个新的Action,名为Music,不接受任何参数,并返回对应的视图,视图显示“顶级打野” +![](./imgs/8-9.png) +10.给第8题的新控制器,新增一个Action,名为List,不接受任何参数,并返回对应视图,视图显示一个经典CRUD界面 +![](./imgs/table.png) +11.新增一个控制器,名为Products,该控制器具有一个名为Edit的Action,这个Action接受一个int类型的参数id,显示这个id +![](./imgs/8-11.png) +12.在11题的新控制器中,新增一个名为Create的Action,该Action接受一个类型为Students(有姓名、年龄、体长属性)的参数,并展示该参数的姓名属性 +![](./imgs/8-12.png) \ No newline at end of file diff --git "a/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/1.png" "b/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/1.png" new file mode 100644 index 0000000000000000000000000000000000000000..e9422fd9a18086c901ce4973d6cda117943a565c Binary files /dev/null and "b/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/1.png" differ diff --git "a/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/2.png" "b/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/2.png" new file mode 100644 index 0000000000000000000000000000000000000000..e2401ace2a5cd696fbaf8953219b4383b377dc9c Binary files /dev/null and "b/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/2.png" differ diff --git "a/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/3.png" "b/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/3.png" new file mode 100644 index 0000000000000000000000000000000000000000..9a7b5e3aedcf34807d76cc896c47b66772c45bb9 Binary files /dev/null and "b/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/3.png" differ diff --git "a/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/4.png" "b/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/4.png" new file mode 100644 index 0000000000000000000000000000000000000000..bf8809eebe6a70fbb1cbc5e301cad818a90c8098 Binary files /dev/null and "b/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/4.png" differ diff --git "a/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/5-1.png" "b/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/5-1.png" new file mode 100644 index 0000000000000000000000000000000000000000..7b302524128407fcfb58cdb8c34967060936800c Binary files /dev/null and "b/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/5-1.png" differ diff --git "a/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/5-10.png" "b/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/5-10.png" new file mode 100644 index 0000000000000000000000000000000000000000..971832994eb0aaf856e927093c3723088be0a429 Binary files /dev/null and "b/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/5-10.png" differ diff --git "a/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/5-11.png" "b/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/5-11.png" new file mode 100644 index 0000000000000000000000000000000000000000..ab95786a588288a610d2588f510be1a23291182b Binary files /dev/null and "b/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/5-11.png" differ diff --git "a/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/5-12.png" "b/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/5-12.png" new file mode 100644 index 0000000000000000000000000000000000000000..fcac67d2b8c94ef161a315e9bc71f5102fb327e2 Binary files /dev/null and "b/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/5-12.png" differ diff --git "a/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/5-13.png" "b/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/5-13.png" new file mode 100644 index 0000000000000000000000000000000000000000..7417f8f4792bbe08594a8892ff3c8c0eef7d50d8 Binary files /dev/null and "b/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/5-13.png" differ diff --git "a/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/5-14.png" "b/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/5-14.png" new file mode 100644 index 0000000000000000000000000000000000000000..431e4e00fafb9c0b48b5cf93c341dc0f1cd96d61 Binary files /dev/null and "b/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/5-14.png" differ diff --git "a/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/5-15.png" "b/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/5-15.png" new file mode 100644 index 0000000000000000000000000000000000000000..b42487d7faa67ccf662d51818e4c6c5f0aefe567 Binary files /dev/null and "b/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/5-15.png" differ diff --git "a/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/5-16.png" "b/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/5-16.png" new file mode 100644 index 0000000000000000000000000000000000000000..bc47e684d84c31a92e843e8c19382600fe57529b Binary files /dev/null and "b/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/5-16.png" differ diff --git "a/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/5-17.png" "b/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/5-17.png" new file mode 100644 index 0000000000000000000000000000000000000000..6ed2cf2d5ea37aacd581abeecd97dad84eb39ca9 Binary files /dev/null and "b/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/5-17.png" differ diff --git "a/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/5-18.png" "b/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/5-18.png" new file mode 100644 index 0000000000000000000000000000000000000000..1766edda8a0b5922b3db471a0cb866d9c4617199 Binary files /dev/null and "b/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/5-18.png" differ diff --git "a/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/5-19.png" "b/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/5-19.png" new file mode 100644 index 0000000000000000000000000000000000000000..458926e642df013a9ba3c9afa567f1dc41c7c0e4 Binary files /dev/null and "b/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/5-19.png" differ diff --git "a/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/5-2.png" "b/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/5-2.png" new file mode 100644 index 0000000000000000000000000000000000000000..9ed5e92e4684974a464e43f7f2a6910f600fe139 Binary files /dev/null and "b/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/5-2.png" differ diff --git "a/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/5-20.png" "b/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/5-20.png" new file mode 100644 index 0000000000000000000000000000000000000000..9f95504e5ae4a6c3a4faba00d1914fda4f55bbf3 Binary files /dev/null and "b/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/5-20.png" differ diff --git "a/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/5-21.png" "b/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/5-21.png" new file mode 100644 index 0000000000000000000000000000000000000000..ccf25f696dc6f3cc496762c659d71ff1121c5e7a Binary files /dev/null and "b/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/5-21.png" differ diff --git "a/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/5-22.png" "b/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/5-22.png" new file mode 100644 index 0000000000000000000000000000000000000000..ccf25f696dc6f3cc496762c659d71ff1121c5e7a Binary files /dev/null and "b/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/5-22.png" differ diff --git "a/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/5-23.png" "b/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/5-23.png" new file mode 100644 index 0000000000000000000000000000000000000000..85e35d30b696658b0f74f00ef0c8156017d2b493 Binary files /dev/null and "b/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/5-23.png" differ diff --git "a/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/5-3.png" "b/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/5-3.png" new file mode 100644 index 0000000000000000000000000000000000000000..98eeb3c0ec20dbc0c20f65609fb5694cf069bbe2 Binary files /dev/null and "b/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/5-3.png" differ diff --git "a/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/5-4.png" "b/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/5-4.png" new file mode 100644 index 0000000000000000000000000000000000000000..e5ccb0509f031dc7359158d141b1235173a4a33e Binary files /dev/null and "b/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/5-4.png" differ diff --git "a/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/5-5.png" "b/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/5-5.png" new file mode 100644 index 0000000000000000000000000000000000000000..8fed7f6de9c839c967168cfe6d86208cf9ccc584 Binary files /dev/null and "b/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/5-5.png" differ diff --git "a/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/5-7.png" "b/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/5-7.png" new file mode 100644 index 0000000000000000000000000000000000000000..ae2a80ae405796c4c53d229016f21fcee3c47d30 Binary files /dev/null and "b/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/5-7.png" differ diff --git "a/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/5-8.png" "b/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/5-8.png" new file mode 100644 index 0000000000000000000000000000000000000000..22e92479623b09719e3c9c81e7ebdfc99d0c441d Binary files /dev/null and "b/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/5-8.png" differ diff --git "a/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/5-9.png" "b/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/5-9.png" new file mode 100644 index 0000000000000000000000000000000000000000..0724a808c76782163ff2b09a847aac8d267b40da Binary files /dev/null and "b/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/5-9.png" differ diff --git "a/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/8-1.png" "b/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/8-1.png" new file mode 100644 index 0000000000000000000000000000000000000000..31ac69b412f6e5ac90fa474162736ee1f0e8ee44 Binary files /dev/null and "b/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/8-1.png" differ diff --git "a/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/8-10.png" "b/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/8-10.png" new file mode 100644 index 0000000000000000000000000000000000000000..1e14a6bb2cee76cf5849900baf2890f482d8183f Binary files /dev/null and "b/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/8-10.png" differ diff --git "a/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/8-11.png" "b/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/8-11.png" new file mode 100644 index 0000000000000000000000000000000000000000..f9f4d1e41af2a4296e0f87e51ca01a6cb0e2c036 Binary files /dev/null and "b/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/8-11.png" differ diff --git "a/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/8-12.png" "b/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/8-12.png" new file mode 100644 index 0000000000000000000000000000000000000000..fe5d27b8e28e9180fd60e51e1740a4c28a6c4621 Binary files /dev/null and "b/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/8-12.png" differ diff --git "a/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/8-2.png" "b/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/8-2.png" new file mode 100644 index 0000000000000000000000000000000000000000..bd04af5d51ce7d9bc3a77a35701120845cf3dc5b Binary files /dev/null and "b/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/8-2.png" differ diff --git "a/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/8-3.png" "b/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/8-3.png" new file mode 100644 index 0000000000000000000000000000000000000000..07197e06cdee94e4783ca4fdb687a418a9d3cbff Binary files /dev/null and "b/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/8-3.png" differ diff --git "a/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/8-4.png" "b/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/8-4.png" new file mode 100644 index 0000000000000000000000000000000000000000..9b67a6f2edf8b48ee3ffe25c33e664bcdbaca593 Binary files /dev/null and "b/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/8-4.png" differ diff --git "a/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/8-5.png" "b/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/8-5.png" new file mode 100644 index 0000000000000000000000000000000000000000..9b67a6f2edf8b48ee3ffe25c33e664bcdbaca593 Binary files /dev/null and "b/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/8-5.png" differ diff --git "a/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/8-6.png" "b/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/8-6.png" new file mode 100644 index 0000000000000000000000000000000000000000..577e959b14d9d026440e91afc78777e292669523 Binary files /dev/null and "b/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/8-6.png" differ diff --git "a/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/8-7.png" "b/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/8-7.png" new file mode 100644 index 0000000000000000000000000000000000000000..2a655e34b55a0cd3630493e2811832c290c050d0 Binary files /dev/null and "b/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/8-7.png" differ diff --git "a/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/8-8.png" "b/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/8-8.png" new file mode 100644 index 0000000000000000000000000000000000000000..853a02e6f311d8885fef612c12f73c19ca86eef9 Binary files /dev/null and "b/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/8-8.png" differ diff --git "a/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/8-9.png" "b/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/8-9.png" new file mode 100644 index 0000000000000000000000000000000000000000..d6dcec24797852af4968c0570c5b50c9de90d509 Binary files /dev/null and "b/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/8-9.png" differ diff --git "a/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/table.png" "b/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/table.png" new file mode 100644 index 0000000000000000000000000000000000000000..76570b0f78fd019f630a6a8ef0565482d83efad9 Binary files /dev/null and "b/\350\214\203\351\270\277\351\233\257/20241119-\346\216\247\345\210\266\345\231\250/imgs/table.png" differ diff --git "a/\350\214\203\351\270\277\351\233\257/20241121-\346\216\247\345\210\266\345\231\250\345\217\202\346\225\260/20241121\346\216\247\345\210\266\345\231\250\345\217\202\346\225\260\345\217\212\350\277\224\345\233\236\345\200\274.md" "b/\350\214\203\351\270\277\351\233\257/20241121-\346\216\247\345\210\266\345\231\250\345\217\202\346\225\260/20241121\346\216\247\345\210\266\345\231\250\345\217\202\346\225\260\345\217\212\350\277\224\345\233\236\345\200\274.md" new file mode 100644 index 0000000000000000000000000000000000000000..b14cfa86fd6c003ee5e18b6212b9d5aa2e6133c8 --- /dev/null +++ "b/\350\214\203\351\270\277\351\233\257/20241121-\346\216\247\345\210\266\345\231\250\345\217\202\346\225\260/20241121\346\216\247\345\210\266\345\231\250\345\217\202\346\225\260\345\217\212\350\277\224\345\233\236\345\200\274.md" @@ -0,0 +1,171 @@ +# ASP.NET Core MVC 控制器参数及返回值 + +## 1. 控制器参数概述 +控制器中的动作方法可以接收参数,这些参数可以通过不同的方式绑定,如路由数据、查询字符串、表单数据等。 + +## 2. 参数绑定方式 + +### 2.1 路由参数 +- 使用路由定义中指定的参数,可以通过控制器动作的参数直接获取。 + +```csharp +[HttpGet("{id}")] +public IActionResult GetItem(int id) +{ + // 使用id参数 + return View(); +} +``` + +### 2.2 查询字符串 +- 查询字符串参数可以直接在方法参数中定义。 + +```csharp +[HttpGet] +public IActionResult Search(string query) +{ + // 使用query参数 + return View(); +} +``` + +### 2.3 表单数据 +- 使用`[FromForm]`特性指明参数值来自表单提交。 + +```csharp +[HttpPost] +public IActionResult Create([FromForm] Item item) +{ + // 处理item对象 + return RedirectToAction("Index"); +} +``` + +### 2.4 JSON 数据 +- 使用`[FromBody]`特性,参数从请求的主体中读取(通常用于 API)。 + +```csharp +[HttpPost] +public IActionResult Create([FromBody] Item item) +{ + // 处理item对象 + return Ok(); +} +``` + +### 2.5 组合绑定 +- 支持多种方式组合使用。 + +```csharp +public IActionResult Update(int id, [FromBody] Item item) +{ + // 更新逻辑 + return NoContent(); +} +``` + +## 3. 控制器返回值 + +### 3.1 IActionResult +- 最常用的返回类型,支持多种结果类型的返回。 + +```csharp +public IActionResult Index() +{ + return View(); +} +``` + +### 3.2 具体返回类型 +- **ViewResult**: 返回一个视图。 + +```csharp +public IActionResult About() +{ + return View(); +} +``` + +- **RedirectResult**: 将用户重定向到指定的URL。 + +```csharp +public IActionResult RedirectToHome() +{ + return RedirectToAction("Index", "Home"); +} +``` + +- **JsonResult**: 返回JSON格式的数据。 + +```csharp +public IActionResult GetJson() +{ + var data = new { Name = "Item", Value = 100 }; + return Json(data); +} +``` + +- **ContentResult**: 返回纯文本或HTML内容。 + +```csharp +public IActionResult GetText() +{ + return Content("Hello World!", "text/plain"); +} +``` + +- **FileResult**: 返回文件。 + +```csharp +public IActionResult DownloadFile() +{ + var filePath = "path/to/file.txt"; + return PhysicalFile(filePath, "application/octet-stream", "downloadedfile.txt"); +} +``` + +### 3.3 状态码返回 +- 可以直接返回HTTP状态码。 + +```csharp +public IActionResult NotFoundExample() +{ + return NotFound(); +} +``` + +## 4. 异常处理 +- 在控制器中,可以使用`try-catch`块来捕获异常,并返回相应的结果。 + +```csharp +public IActionResult GetById(int id) +{ + try + { + var item = _service.GetById(id); + if (item == null) return NotFound(); + + return Ok(item); + } + catch (Exception ex) + { + return StatusCode(500, ex.Message); + } +} +``` + +## 5. 小技巧 +- 通过使用默认值、可选参数和`ModelState`来进行更复杂的参数验证和处理。 + +```csharp +public IActionResult Update(int id, [FromBody] Item item) +{ + if (!ModelState.IsValid) + { + return BadRequest(ModelState); + } + // 处理更新逻辑 + return NoContent(); +} +``` + diff --git "a/\350\214\203\351\270\277\351\233\257/20241122-\350\247\206\345\233\276/20241122\350\247\206\345\233\276.md" "b/\350\214\203\351\270\277\351\233\257/20241122-\350\247\206\345\233\276/20241122\350\247\206\345\233\276.md" new file mode 100644 index 0000000000000000000000000000000000000000..2adf41861862cd88e89de164b1fa0d3df0bc712f --- /dev/null +++ "b/\350\214\203\351\270\277\351\233\257/20241122-\350\247\206\345\233\276/20241122\350\247\206\345\233\276.md" @@ -0,0 +1,444 @@ + +# ASP.NET Core MVC 视图 + +## 1. 视图概述 +- 视图是ASP.NET Core MVC中的一部分,用于呈现模型数据并生成用户界面。 +- 视图通常使用Razor语法(.cshtml文件)编写。 + +## 2. 视图类型 + +### 2.1 强类型视图 +- 强类型视图是指视图直接与模型类型绑定,使得在视图中可以直接访问模型属性。 + +```csharp +@model MyApp.Models.Item + +

@Model.Name

+

价格:@Model.Price

+``` + +### 2.2 动态类型视图 +- 动态类型视图不使用强类型绑定,而是通过`ViewBag`或`ViewData`动态传递数据。 + +```csharp +@{ + var name = ViewBag.Name; +} + +

@name

+``` + +## 3. 创建视图 + +### 3.1 在控制器中返回视图 +- 可以使用`View`方法返回一个视图。 + +```csharp +public IActionResult Index() +{ + var item = new Item { Name = "Sample Item", Price = 100 }; + return View(item); // 返回强类型视图 +} +``` + +### 3.2 选择视图 +- 可以通过`View("ViewName")`方法指定想要返回的视图名称。 + +```csharp +public IActionResult CustomView() +{ + return View("MyCustomView"); +} +``` + +## 4. 视图布局 +- 视图布局文件(通常是**_Layout.cshtml**)用于定义共享的UI结构,例如头部、尾部和导航栏。 + +### 4.1 定义布局 +- 在视图文件中,使用`Layout`属性指定布局文件。 + +```csharp +@{ + Layout = "_Layout"; +} +``` + +### 4.2 使用`RenderBody` +- 在布局中,使用`@RenderBody()`来渲染主视图内容。 + +```html + + + + @ViewData["Title"] + + +
+

网站标题

+
+ +
+ @RenderBody() +
+ + + + +``` + +## 5. 部分视图 +- 部分视图(Partial Views)是重用的可重用视图组件,用于将功能和显示逻辑分离。 + +### 5.1 创建部分视图 +- 创建一个以 **_** 开头的视图文件,例如 **_ItemPartial.cshtml**。 + +```csharp +@model MyApp.Models.Item + +
+

@Model.Name

+

价格:@Model.Price

+
+``` + +### 5.2 使用部分视图 +- 在主视图中使用`Html.Partial`或`Html.RenderPartial`渲染部分视图。 + +```csharp +@Html.Partial("_ItemPartial", Model) +``` + +## 6. 使用Razor语法 +- Razor语法用于在视图中混合HTML和C#代码。 + +### 6.1 常用语法 +- `@model`:声明模型类型。 +- `@{ }`:运行C#代码块。 +- `@if`、`@for`、`@foreach`:控制流语句。 + +```csharp +@foreach (var item in Model.Items) +{ +

@item.Name

+} +``` + +## 7. 视图组件 +- 视图组件是可以重用的UI组件,类似于部分视图,但支持更复杂的逻辑。 + +### 7.1 创建视图组件 +- 定义继承自 `ViewComponent` 的类。 + +```csharp +public class MyViewComponent : ViewComponent +{ + public IViewComponentResult Invoke(string name) + { + var item = GetItemByName(name); + return View(item); + } +} +``` + +### 7.2 使用视图组件 +- 在视图中调用视图组件。 + +```csharp +@await Component.InvokeAsync("MyViewComponent", new { name = "Sample Item" }) +``` + +## 8. 数据注入 +- 可以在视图中使用依赖注入获取服务。 + +```csharp +@inject IMyService MyService + +

@MyService.GetData()

+``` +# 专项练习-控制器传参 +## 简单参数传递 在一个叫Blog控制器中,定义一个叫Index的Action,并且传递一个int类型的值,id为变量名 +```csharp + public class BlogController : Controller + { + public IActionResult Index(int id) + { + return Content(id.ToString) + } + + } +``` +## 简单参数传递 在一个叫Blog控制器中,定义一个叫Index_2的Action,并且传递一个string类型的值,id为变量名 +```csharp +public class BlogController : Controller + { + + public IActionResult Index_2(string id) + { + return Content(id) + } + + } +``` +## 简单参数传递 在一个叫Blog控制器中,定义一个叫Index_3的Action,并且传递一个string类型的值,name为变量名 +```csharp +public class BlogController : Controller + { + public IActionResult Index_3(string name) + { + return Content(name) + } + } +``` +## 复杂参数传递 在一个叫Blog的控制器中,定义一个名为Create的Action,并且传递一个BlogCreateDto类型的值,blogCreateDto为变量名 + +PS BlogCreateDto类型具有Title、Author、Content自动属性 +```csharp +public class BlogController : Controller + { + [HttpPost] + public IActionResult Create([FromBody] BlogCreateDto blogCreateDto){ + + return Content(blogCreateDto.Title) + } + + public class BlogCreateDto{ + + //BlogCreateDto类型具有Title、Author、Content自动属性 + public string Title{get;set;}=null!; + public string Author{get;set;}=null!; + public string Content{get;set;}=null!; + } + } +``` +## 复杂参数传递 在一个叫Blog的控制器中,定义一个名为Create_1的Action,并且传递一个Products类型的值,productCreateDto为变量名 + +PS Products类型具有Name、Price、Stock自动属性 +```csharp + public class BlogController : Controller + { + [HttpPost] + public IActionResult Create([FromBody] Products productCreateDto){ + return Content(blogCreateDto.Title) + } + //PS Products类型具有Name、Price、Stock自动属性 + public class Products{ + public string Name{get;set;}=null!; + public string Price{get;set;}=null!; + public string Stock{get;set;}=null!; + } + } +``` +## 复杂参数传递 在一个叫Blog的控制器中,定义一个名为Create_2的Action,并且传递一个Students类型的值,studentCreateDto为变量名 + +PS Students类型具有StudentName、Sex、Age自动属性 +```csharp + public class BlogController : Controller + { + public class BlogContriller : Controller{ + [HttpPost] + public IActionResult Create_2([FromBody] Students studentCreateDto){ + return Content(blogCreateDto.Title) + } + } + public class Students{ + public string Name{get;set;}=null!; + public string Price{get;set;}=null!; + public string Stock{get;set;}=null!; + } + } +``` +# 专项练习-基础能力 +## 生成一个随机整数,范围[0,100],注意是否包含 +```csharp +Random random = new Random(); +int number1 = random.Next(0, 101); +``` +## 生成一个随机整数,范围(0,100],注意是否包含 +```csharp +int number2 = random.Next(1, 100); +``` +## 生成10个随机整数,范围[5,80],注意是否包含 +```csharp +int[] numbers3 = new int[10]; +for (int i = 0; i < numbers3.Length; i++) +{ + numbers3[i] = random.Next(5, 81); +} +``` +## 定义一个字符串,字符串中有100个中文字符,需要从中随机取1个字符串 +```csharp +string chineseCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; +Random random = new Random(); +string oneChineseCharacter = chineseCharacters[random.Next(chineseCharacters.Length)]; + +``` +## 定义一个字符串,字符串中有100个中文字符,需要从中随机取5-50个字符,组成新的字符 +```csharp +int length = random.Next(5, 51); +StringBuilder sb = new StringBuilder(); +for (int i = 0; i < length; i++) +{ + sb.Append(chineseCharacters[random.Next(chineseCharacters.Length)]); +} +string fiveToFiftyChineseCharacters = sb.ToString(); +``` +## 定义2个字符串,第一个字符串中放百家姓,第二个字符串中放中文字符,要求从第一个字符串随机取得一个姓,再从第二个字符串中随机获得1到2个字符组成新字符串,和第一个字符串取得的姓组成一个姓名 +```csharp +string surnames = "刘赵钱孙李周吴郑王冯陈卫蒋沈韩杨"; +string givenNames = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; +int surnameIndex = random.Next(surnames.Length); +string surname = surnames[surnameIndex].ToString(); +int givenNameLength = random.Next(1, 3); +StringBuilder givenNameSb = new StringBuilder(); +for (int i = 0; i < givenNameLength; i++) +{ + givenNameSb.Append(givenNames[random.Next(givenNames.Length)]); +} +string givenName = givenNameSb.ToString(); +string name = surname + givenName; +``` +## 利用以上方法,随机生成100个BlogCreateDto类型(有Title、Author、Content属性)的对象,其中的内容都是随机生成且长度不定,并将其渲染到视图 +```csharp +public class BlogCreateDto +{ + public string Title { get; set; } + public string Author { get; set; } + public string Content { get; set; } +} + +List blogs = new List(); +for (int i = 0; i < 100; i++) +{ + BlogCreateDto blog = new BlogCreateDto + { + Title = GenerateRandomString(10, 50), // 随机标题长度10-50 + Author = name, // 使用上面生成的姓名 + Content = GenerateRandomString(100, 1000) // 随机内容长度100-1000 + }; + blogs.Add(blog); +} + +string GenerateRandomString(int minLength, int maxLength) +{ + StringBuilder contentSb = new StringBuilder(); + int contentLength = random.Next(minLength, maxLength + 1); + for (int i = 0; i < contentLength; i++) + { + contentSb.Append(chineseCharacters[random.Next(chineseCharacters.Length)]); + } + return contentSb.ToString(); +} +``` +# 专项练习-控制器返回值 +## 渲染简单数据到页面 +``` csharp + public class EndController : Controller + { + [HttpGet] + public IActionResult Editone(int id) +{ + var list=new List{ + new(){ + Title="今天星期四", + Author="小怡", + Content="KFC V我50", + Name="Jackson" + }, + new(){ + Title="今天星期四", + Author="小怡", + Content="KFC V我50", + Name="Jackson" + }, + new(){ + Title="今天星期四", + Author="小怡", + Content="KFC V我50", + Name="Jackson" + }, + new(){ + Title="今天星期四", + Author="小怡", + Content="KFC V我50", + Name="Jackson" + }, + new(){ + Title="今天星期四", + Author="小怡", + Content="KFC V我50", + Name="Jackson" + }, + new(){ + Title="今天星期四", + Author="小怡", + Content="KFC V我50", + Name="Jackson" + } + }; + return View(list); +} + + } +public class SimpleViewModel +{ + public string Title { get; set; }=null!; + public string Author { get; set; }=null!; + + public string Content { get; set; }=null!; + + public string Name { get; set; }=null!; + +} + +``` +``` html +@model List + + + + + + + + + + @foreach(var item in @Model){ + + + + + + + + } +
标题作者内容姓名操作
@item.Title@item.Author@item.Content@item.Name + + +
+``` +## 渲染复杂数据到页面 +![](../20241114-文件结构以及注意项/img/table.png) +## 渲染集合数据到页面 +```csharp +public IActionResult CollectionView() +{ + CollectionViewModel model = new CollectionViewModel + { + Items = new List { "Item1", "Item2", "Item3" } + }; + return View(model); +} +``` +```html + +@model YourNamespace.CollectionViewModel + +
    + @foreach (var item in Model.Items) + { +
  • @item
  • + } +
+``` \ No newline at end of file