From 45f07c12d419d13c4c6e72a9f56d705e3a7a8446 Mon Sep 17 00:00:00 2001 From: bmchu070 <1549098850@qq.com> Date: Sun, 24 Nov 2024 22:41:04 +0800 Subject: [PATCH] 20241120mvc --- .../20241118-RESTful.md" | 53 ++++++ ...0\351\200\222\345\217\202\346\225\260 .md" | 26 +++ .../\344\275\234\344\270\232/20241120-mvc.md" | 162 ++++++++++++++++++ 3 files changed, 241 insertions(+) create mode 100644 "\347\216\213\350\212\267\345\256\201/20241118-RESTful.md" create mode 100644 "\347\216\213\350\212\267\345\256\201/20241120-\344\274\240\351\200\222\345\217\202\346\225\260 .md" create mode 100644 "\347\216\213\350\212\267\345\256\201/\344\275\234\344\270\232/20241120-mvc.md" diff --git "a/\347\216\213\350\212\267\345\256\201/20241118-RESTful.md" "b/\347\216\213\350\212\267\345\256\201/20241118-RESTful.md" new file mode 100644 index 0000000..05e60d1 --- /dev/null +++ "b/\347\216\213\350\212\267\345\256\201/20241118-RESTful.md" @@ -0,0 +1,53 @@ +## RESTful API + +RESTful API 是一种基于 HTTP 协议的架构风格,用于构建网络应用的接口。 + +### 1. **GET** + +`GET` 方法用于从服务器获取资源。它不会对服务器上的资源状态造成任何修改,因此它是**安全**的。 + +```http +GET /api/products HTTP/1.1 +Host: example.com +``` + +### 2. **POST** + +`POST` 方法用于向服务器发送数据并创建资源。每次执行 `POST` 请求都会对服务器产生改变。 + +```http +POST /api/products HTTP/1.1 +Host: example.com +Content-Type: application/json + +{ + "name": "AAA", + "price": 999 +} +``` + +### 3. **PUT** + +`PUT` 方法用于更新现有资源,通常是完全替换资源的内容。 + +```http +PUT /api/products/123 HTTP/1.1 +Host: example.com +Content-Type: application/json + +{ + "name": "WWW", + "price": 99999 +} +``` + +### 4. **DELETE** + +`DELETE` 方法用于删除服务器上的指定资源。 + +```http +DELETE /api/products/123 HTTP/1.1 +Host: example.com +``` + +--- \ No newline at end of file diff --git "a/\347\216\213\350\212\267\345\256\201/20241120-\344\274\240\351\200\222\345\217\202\346\225\260 .md" "b/\347\216\213\350\212\267\345\256\201/20241120-\344\274\240\351\200\222\345\217\202\346\225\260 .md" new file mode 100644 index 0000000..1c32329 --- /dev/null +++ "b/\347\216\213\350\212\267\345\256\201/20241120-\344\274\240\351\200\222\345\217\202\346\225\260 .md" @@ -0,0 +1,26 @@ +### URL 请求示例 +当用户访问 `http://localhost:5000/Home/Index/5` 时: +1. **控制器**: 匹配到 `HomeController`。 +2. **操作方法**: 匹配到 `Index` 方法。 +3. **参数**: 路由参数 `id` 的值为 `5`,并传递到 `Index` 方法中。 + +### 参数名的匹配规则 + +在路由模式中,`{id?}` 定义了一个可选参数,其名称为 `id`。控制器方法的参数名必须与此占位符一致,例如: +- 如果路由模式为 `{productId?}`,则方法参数名应为 `productId`。 + +- 参数名不匹配时,将导致请求无法正确解析参数。 + +- `.csproj`是项目文件 +- `properties`文件中可以修改端口号 + +### 传参 + +``` +public class (project新项目命名) : Controller{ + public IActionResult Index(int id) + { + return Content(id.ToString()); + } +} +``` \ No newline at end of file diff --git "a/\347\216\213\350\212\267\345\256\201/\344\275\234\344\270\232/20241120-mvc.md" "b/\347\216\213\350\212\267\345\256\201/\344\275\234\344\270\232/20241120-mvc.md" new file mode 100644 index 0000000..311a33d --- /dev/null +++ "b/\347\216\213\350\212\267\345\256\201/\344\275\234\344\270\232/20241120-mvc.md" @@ -0,0 +1,162 @@ +### 1. 创建一个控制台项目 + +```bash +dotnet new console +``` +### 2. 创建一个控制项目,项目名称Blog + +```bash +dotnet new console -n Blog +``` +### 3. 创建一个控制台项目,输出到Blog目录 + +```bash +dotnet new console -o Blog +``` +### 4. 创建一个MVC项目,指定项目名称 + +```bash +dotnet new mvc -n Blog +``` +### 5. 创建一个MVC项目,指定输出目录 + +```bash +dotnet new mvc -o BlogDir +``` +### 6. 创建一个带解决方案,其下有一个MVC项目,3个类库项目的“综合项目” + +```bash +dotnet new classlib -n Zonghe1 +dotnet new classlib -n Zonghe2 +dotnet new classlib -n Zonghe3 +``` + +### 7. 创建一个项目,在默认控制器(Home)下,新增一个Action方法,名为Ok,同时为其创建对应视图以显示这个视图 + +```csharp +public class HomeController : Controller +{ + public IActionResult Ok() + { + return View(); + } +} +``` + +```html +

Ok

+

This is the Ok view.

+``` + +### 8. 创建一个项目,创建一个新的控制器,名为Blogs,新的控制器拥有一个名为Index的Action,该方法返回一个视图,视图显示“神级预判” + +```csharp +public class BlogsController : Controller +{ + public IActionResult Index() + { + return View(); + } +} +``` + +```html +

神级预判

+``` + +### 9. 给第8题的新控制器,添加一个新的Action,名为Music,不接受任何参数,并返回对应的视图,视图显示“顶级打野” + +```csharp +public class BlogsController : Controller +{ + public IActionResult Index() + { + return View(); + } + + public IActionResult Music() + { + return View(); + } +} +``` + +```html +

顶级打野

+``` + +### 10. 给第8题的新控制器,新增一个Action,名为List,不接受任何参数,并返回对应视图,视图显示一个经典CRUD界面 + +```csharp +public class BlogsController : Controller +{ + public IActionResult Index() + { + return View(); + } + + public IActionResult List() + { + return View(); + } +} +``` + +### 11. 新增一个控制器,名为Products,该控制器具有一个名为Edit的Action,这个Action接受一个int类型的参数id,显示这个id + +```csharp +public class ProductsController : Controller +{ + public IActionResult Edit(int id) + { + ViewBag.Id = id; + return View(); + } +} +``` + +```html +

Edit

+

@ViewBag.Id

+``` + +### 12. 在11题的新控制器中,新增一个名为Create的Action,该Action接受一个类型为Students(有姓名、年龄、体长属性)的参数,并展示该参数的姓名属性 + +```csharp +using Microsoft.AspNetCore.Mvc; +using project.Models; + +namespace project.Controllers +{ + public class ProductsController : Controller + { + public class Students + { + public required string Name { get; set; } + public int Age { get; set; } + public double Height { get; set; } + } + + [HttpGet] + public IActionResult Create() + { + return View(); + } + + [HttpPost] + public IActionResult Create(Students student) + { + ViewBag.StudentName = student.Name; + return View(student); + } + } +} + +``` + +```html +

Create Student

+

学生姓名:@ViewBag.StudentName

+

年龄:@Model.Age

+

身高:@Model.Height

+``` \ No newline at end of file -- Gitee