From 24615c9acba923e5bc0c7b2390162faedfe7f875 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 1 Dec 2024 17:42:36 +0800 Subject: [PATCH] zx --- ...36\345\200\274\347\261\273\345\236\213.md" | 157 +++++++++++++++++ ...24\342\200\224\350\247\206\345\233\276.md" | 159 ++++++++++++++++++ ...24\342\200\224\345\242\236\345\212\240.md" | 98 +++++++++++ 3 files changed, 414 insertions(+) create mode 100644 "\345\221\250\346\227\255/20241125\342\200\224\342\200\224\350\277\224\345\233\236\345\200\274\347\261\273\345\236\213.md" create mode 100644 "\345\221\250\346\227\255/20241127\342\200\224\342\200\224\350\247\206\345\233\276.md" create mode 100644 "\345\221\250\346\227\255/20241129\342\200\224\342\200\224\345\242\236\345\212\240.md" diff --git "a/\345\221\250\346\227\255/20241125\342\200\224\342\200\224\350\277\224\345\233\236\345\200\274\347\261\273\345\236\213.md" "b/\345\221\250\346\227\255/20241125\342\200\224\342\200\224\350\277\224\345\233\236\345\200\274\347\261\273\345\236\213.md" new file mode 100644 index 0000000..eda1bce --- /dev/null +++ "b/\345\221\250\346\227\255/20241125\342\200\224\342\200\224\350\277\224\345\233\236\345\200\274\347\261\273\345\236\213.md" @@ -0,0 +1,157 @@ +# Action的返回值类型 + +## 常规数据类型 + +- int + +- string + +- IList<> + +## 朴实的ActionResult + +所有的 Result 都派生自 ActionResult抽象类,因此 ActionResult 作为基类提供了最基础的功能,ActionResult 是一个抽象类,其声明如下: + +```c# +public abstract class ActionResult { + + public abstract void ExecuteResult(ControllerContext context); +} +``` + +*ActionResult 返回响应的状态码:200,301,401,404,500等* + +## ContentResult + +ContentResult用于将字符串直接向客户端输出。ContentResult的ExecuteResult方法实际上是调用了 Response.Write( string… ),输入并无特别之处,但是在 ASP 时代,这个Response.Write() 却是可以纵横页面。从输出一个简单的字符串到整个页面,Response.Write()都能胜任,所以ContentResult显得特别强大: + +```c# +public override void ExecuteResult(ControllerContext context) { + +if (context == null) { + +throw new ArgumentNullException("context"); + +} + +HttpResponseBase response = context.HttpContext.Response; + +if (!String.IsNullOrEmpty(ContentType)) { + + response.ContentType = ContentType; + +} + +if (ContentEncoding != null) { + + response.ContentEncoding = ContentEncoding; + + } + +if (Content != null) { + + response.Write(Content); + + } + +} + +``` +## JsonResult + +JsonResult首先将指定的对象序列化为Json字符串,然后将字符串写入到HTTP输出流。撇开对象序列化为Json字符串这一过程,实际上与ContentResult其实是一样的,因为JsonResult与ContentResult都是调用Response.Write()向HTTP输出流写入一些内容. + +# 作业 + +## 基础能力 + +### 1.生成一个随机整数,范围[0,100],注意是否包含 +```cs +public IActionResult Index() +{ + Random random = new Random(); + int randomNumber = random.Next(0, 101); + return View(randomNumber); +} +``` +### 2.生成一个随机整数,范围(0,100],注意是否包含 +```cs + +public IActionResult Index_2() +{ + Random random = new Random(); + int randomNumber = random.Next(1, 101); + return View(randomNumber); +} +``` +### 3.生成10个随机整数,范围[5,80],注意是否包含 +```cs +public IActionResult Index_3() +{ + Random random = new Random(); + List Numbers = new List(); + for (int i = 0; i < 10; i++) + { + int num = random.Next(5, 81); + Numbers.Add(num); + } + return View(randomNumbers); +} +``` +### 4.定义一个字符串,字符串中有100个中文字符,需要从中随机取1个字符串 +```cs +public IActionResult Index_4() +{ + string str = "大多数是的阿萨加速度计文件诋毁我是女我的骄傲为大家偶氮基吴华峰数据库我的骄傲看i为大家阿达我hi带我都我进去的hi去外地哦好几千的我电话奥委会嗲嗲和我的我电话i我i的皇帝和我的海外海动画地位海外"; + // 创建Random对象 + Random random = new Random(); + char Chinese = str[random.Next(str.Length)]; + + return View(Chinese); +} +``` +### 5.定义一个字符串,字符串中有100个中文字符,需要从中随机取5-50个字符,组成新的字符 +```cs +public ActionResult Index_5() +{ + string str = "哦请问姐姐哦对骄傲动物回家全家都就安东街傲娇欧文基督教哦我就欧锦欧文就解耦哦今晚案件哦怕我的卡片我我看我脾气恐怕恐怕看普外科爱哭的叫我就阿文大家阿文扩大我看哦啊我尽快欧文欧文大家傲娇的文件i的你的那位环境"; + Random random = new Random(); + int length = random.Next(5, 51); + // 随机选择字符并组成新的字符串 + string newStr = ""; + for (int i = 0; i < length; i++) + { + int index = random.Next(str.Length); + newStr += str[index]; + } + ViewBag.NewStr = newStr; + + return View(); +} +``` +### 6.定义2个字符串,第一个字符串中放百家姓,第二个字符串中放中文字符,要求从第一个字符串随机取得一个姓,再从第二个字符串中随机获得1到2个字符组成新字符串,和第一个字符串取得的姓组成一个姓名 + +```cs +public IActionResult Index_6() +{ + // 定义包含百家姓的字符串 + string names = "赵,钱,孙,李,周,吴,郑,王,冯,陈,褚,卫,蒋,沈,韩,杨,朱,秦,尤,许,何,吕,施,张,孔,曹,严,华,金,魏,陶,姜,戚,谢,邹,喻,柏,水,窦,章,云,苏,潘,葛,奚,范,彭,郎,鲁,韦,昌,马,苗,凤,花,方,俞,任,袁,柳,酆,鲍,史,唐,费,廉,岑,薛,雷,贺,倪,汤,滕,殷,罗,毕,郝,邬,安,常,乐,于,时,傅,皮,卞,齐,康,伍,余,元,卜,顾,孟,平,黄,和,穆,萧,尹,姚,邵,湛,汪,祁"; + // 定义包含中文字符的字符串 + string chineseChars = "哦请问姐姐哦对骄傲动物回家全家都就安东街傲娇欧文基督教哦我就欧锦欧文就解耦哦今晚案件哦怕我的卡片我我看我脾气恐怕恐怕看普外科爱哭的叫我就阿文大家阿文扩大我看哦啊我尽快欧文欧文大家傲娇的文件i的你的那位环境"; + Random random = new Random(); + string[] arrNames = names.Split(','); + string Dname = arrNames[random.Next(arrNames.Length)]; + // 随机选择1到2个中文字符 + int count = random.Next(1, 3); + string SName = ""; + for (int i = 0; i < count; i++) + { + int index = random.Next(chineseChars.Length); + SName += chineseChars[index]; + } + string name = Dname + SName; + ViewBag.name = name; + + return View(); +} +``` \ No newline at end of file diff --git "a/\345\221\250\346\227\255/20241127\342\200\224\342\200\224\350\247\206\345\233\276.md" "b/\345\221\250\346\227\255/20241127\342\200\224\342\200\224\350\247\206\345\233\276.md" new file mode 100644 index 0000000..e1bf4b6 --- /dev/null +++ "b/\345\221\250\346\227\255/20241127\342\200\224\342\200\224\350\247\206\345\233\276.md" @@ -0,0 +1,159 @@ +## 渲染 + +- 简单数据在视图的渲染和展示 +- 复杂数据在视图的渲染和展示 +- 集合数据在视图的渲染和展示 + +## Razor + +Razor 支持 C#,并使用` @ `符号从 HTML 转换为 C#。 Razor 计算 C# 表达式,并将它们呈现在 HTML 输出中。 + +当 @ 符号后跟 Razor 保留关键字时,它会转换为 Razor 特定标记. + +### 隐式 Razor 表达式 + +以 @ 开头,后跟 C# 代码。隐式表达式不能包含空格,但 C# **await** 关键字除外。不能包含 C# 泛型,因为括号 (<>) 内的字符会被解释为 HTML 标记。 + +### 显式 Razor 表达式 + +由 @ 符号和平衡圆括号组成。 + +## 内置ASP.NET Core标记帮助程序--定位点 + +定位点标记帮助程序可通过添加新属性来增强标准的 HTML 定位点 () 标记。 按照约定,属性名称将使用前缀 asp-。 asp- 属性的值决定呈现的定位点元素的 href 属性值 + +属性: +1. `asp-controller`:可分配用于生成 URL 的控制器。 +2. `asp-action `:属性值表示生成的 href 属性中包含的控制器操作名称。 +3. `asp-all-route-data` :支持创建键值对字典。 键是参数名称,值是参数值。 + +# 作业 +## 1 +```cs +public string Index() +{ + return "简单数据类型"; +} +``` + +## 2 +```cs +public IActionResult Index_2() +{ + var list = new BlogCreateDto + { + Title="流行歌曲", + Author="吴亦凡", + Content="女孩" + }; + + return View(list); +} +``` + +## 3 +```cs +public IActionResult Index_3() +{ + var list = new List + { + new BlogCreateDto + { + Title="流行歌曲", + Author="吴亦凡", + Content="bdagirl" + }, + new BlogCreateDto + { + Title="流行歌曲", + Author="吴亦凡", + Content="bdagirl" + }, + new BlogCreateDto + { + Title="流行歌曲", + Author="吴亦凡", + Content="bdagirl" + }, + }; + return View(list); +} +``` +```html + + + + + + + + @foreach(var item in @Model) + { + + + + + + + } +
标题作者内容操作
@item.Title@item.Author@item.Content + + +
+``` + +## 4 +```cs +public IActionResult Index_4() +{ + var blog = new Blog + { + Title="流行歌曲", + Author="马甲其", + Content="ikun", + BlogCreateDto = new List + { + new BlogCreateDto + { + Title="流行歌曲", + Author="吴亦凡", + Content="八嘎" + }, + new BlogCreateDto + { + Title="流行歌曲", + Author="蔡徐坤", + Content="及你太美" + }, + } + }; + return View(blog); +} +``` +```html +

@Model.Title

+

@Model.Author

+

@Model.Content

+ +

BlogCreateDto:

+ + + + + + + + @foreach(var item in @Model.BlogCreateDto) + { + + + + + + + } +
标题作者内容操作
@item.Title@item.Author@item.Content + + +
+``` \ No newline at end of file diff --git "a/\345\221\250\346\227\255/20241129\342\200\224\342\200\224\345\242\236\345\212\240.md" "b/\345\221\250\346\227\255/20241129\342\200\224\342\200\224\345\242\236\345\212\240.md" new file mode 100644 index 0000000..8378260 --- /dev/null +++ "b/\345\221\250\346\227\255/20241129\342\200\224\342\200\224\345\242\236\345\212\240.md" @@ -0,0 +1,98 @@ +## 实现 增 操作 + +### 定义一个模型Models,模拟数据库 + +1. 定义一个数据库的表 + +2. 静态构造函数(只执行一次,内容如果修改,则需要重新跑) + + 1) 初始化静态属性 + + - 直接初始化:`public static List Blog = new List{"value1","value2"...};` + + - **使用静态构造函数** + + - 延迟初始化 + + - 使用属性初始化器 + + 2) 给这个集合属性,填入一些内容 + +```cs + +public static class Db +{ + public static List Blog{get;set;} + + static Db() + { + Blog = new List(); + + for (int i = 0; i < 15; i++) + { + var tmp = new Blog + { + Id = i + 1,... + }; + + Blog.Add(tmp); + } + } +} + +``` + +### 创建视图,展示内容 + +1. 使用``标签展现 增删改 + + ```html + 新增 + ``` + +2. `asp-controller`:指定表单提交的 **目标控制器** + +3. `asp-action`:指定表单提交时应该调用的控制器中的 **动作(方法)** + +4. `asp-route-*`:通常与以上两个属性配合使用,便在创建表单或链接时能够自动将模型的状态或路由*参数*传递给控制器的特定动作。-id就是传递相应行的id号 + + ```html + 编辑 + ``` + +### 跳转至增加页面 + +1. 页面显示form表单,**一定要声明Models** + +```html + +@model Blogs.Models.Blog; + +
+
+
+
+ +
+ +``` + +### 使用post请求,添加到表单 + +1. 获取表单中最大的ID数,,方便程序在添加中自行增加Id数:`Max()方法` + +2. 测试添加内容是否有被获取到:`return Content(JsonSerializer.Serialize(input))` + +3. 将内容添加至表单 + +```cs +[HttpPost] + +public IActionResult Create(Blog input) +{ + var maxId = Db.Blog.Select(t => t.Id).Max(); + input.Id = maxId + 1; + Db.Blog.Add(input); + return RedirectToAction("Index"); +} +``` \ No newline at end of file -- Gitee