From db4ccbb606e55fdcf885000d8d7300feb9f5aa4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=AE=A1=E6=98=9F=E8=88=AA?= Date: Sun, 24 Nov 2024 21:34:21 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../20241119-\347\273\203\344\271\240.md" | 86 ++++++++++++++ .../20241121-\347\254\224\350\256\260.md" | 9 ++ ...72\347\241\200\350\203\275\345\212\233.md" | 109 ++++++++++++++++++ ...66\345\231\250\344\274\240\345\217\202.md" | 88 ++++++++++++++ 4 files changed, 292 insertions(+) create mode 100644 "\347\256\241\346\230\237\350\210\252/20241119-\347\273\203\344\271\240.md" create mode 100644 "\347\256\241\346\230\237\350\210\252/20241121-\347\254\224\350\256\260.md" create mode 100644 "\347\256\241\346\230\237\350\210\252/20241122-\345\237\272\347\241\200\350\203\275\345\212\233.md" create mode 100644 "\347\256\241\346\230\237\350\210\252/20241122-\346\216\247\345\210\266\345\231\250\344\274\240\345\217\202.md" diff --git "a/\347\256\241\346\230\237\350\210\252/20241119-\347\273\203\344\271\240.md" "b/\347\256\241\346\230\237\350\210\252/20241119-\347\273\203\344\271\240.md" new file mode 100644 index 0000000..8c107ec --- /dev/null +++ "b/\347\256\241\346\230\237\350\210\252/20241119-\347\273\203\344\271\240.md" @@ -0,0 +1,86 @@ +#### 笔记 +使用[FromBody]属性来接收数据 +```cs +[HttpPost] +public IActionResult Create([FromBody] Item item) +{ + return View(); +} +``` +#### MVC练习 +1. 创建一个控制台项目,没有任何选项,体会项目名称和什么有关系 + * 项目名称 MyConsoleApp 是项目的唯一标识符,它决定了生成的程序集名称、命名空间以及输出的可执行文件的名称。 + * dotnet new console -n Blog +2. 创建一个控制项目,项目名称Blog + * dotnet new console -n Blog +3. 创建一个控制台项目,输出到Blog目录 + * dotnet new console -o Blog +4. 创建一个MVC项目,指定项目名称 + * dotnet new mvc -n Blog +5. 创建一个MVC项目,指定输出目录 + * dotnet new mvc -o Blog +6. 创建一个带解决方案,其下有一个MVC项目,3个类库项目的“综合项目” + * dotnet new sln -n Blog + * dotnet new mvc -n Blog + * dotnet new classlib -n Bg1 + * dotnet new classlib -n Bg2 + * dotnet new classlib -n Bg3 +7. 创建一个项目,在默认控制器(Home)下,新增一个Action方法,名为Ok,同时为其创建对应视图以显示这个视图 +```cs +public IActionResult Ok() + { + return View(); + } +``` +8. 创建一个项目,创建一个新的控制器,名为Blogs,新的控制器拥有一个名为Index的Action,该方法返回一个视图,视图显示“神级预判” +```cs +using Microsoft.AspNetCore.Mvc; +namespace Blog.Controllers; +public class BlogsController : Controller +{ + public IActionResult Index() + { + return Content("神级预判"); + } +} +``` +9. 给第8题的新控制,添加一个新的Action,名为Music,不接受任何参数,并返回对应的视图,视图显示“顶级打野” +```cs +public IActionResult Index() + { + return Content("顶级打野"); + } +``` +10. 给第8题的新控制器,新增一个Action,名为List,不接受任何参数,并返回对应视图,视图显示一个经典CRUD界面 +```cs + public IActionResult List() + { + return View(); + } +

经典CRUD界面

+``` +11. 新增一个控制器,名为Products,该控制器具有一个名为Edit的Action,这个Action接受一个int类型的参数id,显示这个id +```cs +using Microsoft.AspNetCore.Mvc; +namespace Blog.Controllers; +public class ProductsController : Controller +{ + public IActionResult Edit(int id) + { + return Content($`ID:{id}`); + } +} +``` +12. 在11题的新控制器中,新增一个名为Create的Action,该Action接受一个类型为Students(有姓名、年龄、体长属性)的参数,并展示该参数的姓名属性 +```cs + public IActionResult Create([FromBody] Students stu) + { + return Content(stu.Name) + } + public class Students + { + public string Name { get; set; } = null!; + public int Age { get; set; } = null!; + public string Witch { get; set; } = null!; + } +``` \ No newline at end of file diff --git "a/\347\256\241\346\230\237\350\210\252/20241121-\347\254\224\350\256\260.md" "b/\347\256\241\346\230\237\350\210\252/20241121-\347\254\224\350\256\260.md" new file mode 100644 index 0000000..81c4a01 --- /dev/null +++ "b/\347\256\241\346\230\237\350\210\252/20241121-\347\254\224\350\256\260.md" @@ -0,0 +1,9 @@ +# 笔记 +使用[FromBody]属性来接收数据 +``` +[HttpPost] +public IActionResult Create([FromBody] Item item) +{ + return View(); +} +``` \ No newline at end of file diff --git "a/\347\256\241\346\230\237\350\210\252/20241122-\345\237\272\347\241\200\350\203\275\345\212\233.md" "b/\347\256\241\346\230\237\350\210\252/20241122-\345\237\272\347\241\200\350\203\275\345\212\233.md" new file mode 100644 index 0000000..cb0ddfb --- /dev/null +++ "b/\347\256\241\346\230\237\350\210\252/20241122-\345\237\272\347\241\200\350\203\275\345\212\233.md" @@ -0,0 +1,109 @@ +# 专项练习-基础能力 + +1. 生成一个随机整数,范围[0,100],注意是否包含 +```cs +public IActionResult Number() + { + int number =new Random.Next(0, 101); + return Contene($"随机数:{number}"); + } +``` +2. 生成一个随机整数,范围(0,100],注意是否包含 +```cs +public IActionResult Number2() + { + int number2 =new Random.Next(1, 101); + return Contene($"随机数:{number2}"); + } +``` +3. 生成10个随机整数,范围[5,80],注意是否包含 +```cs +public IActionResult Number3() + { + List number3 = new List(); + for (int i = 0; i < 10; i++) + { + number3.Add(new Random().Next(5, 81)); + } + return View(number3); + } +``` +4. 定义一个字符串,字符串中有100个中文字符,需要从中随机取1个字符串 +```cs +public IActionResult Number4() + { + string cn = "阳光灿烂大地暖绿草如茵花娇艳山河壮丽美如画蓝天白云映水面飞鸟翱翔鱼欢跃春夏秋冬四季转日月星辰耀九天男女老少笑开颜手脚并用勤劳作口耳相传智慧添红黄蓝绿色彩艳大小高低各不同多少长短有分别快慢动静皆自然风雨雷电显神奇木石水火有妙用衣食住行不可少喜怒哀乐情万千诗词歌赋韵味长琴棋书画雅趣生仁义礼智信为本忠孝廉耻心中存团结友爱力量大互帮互助共前行努力奋斗创未来幸福生活乐无边"; + int ch = new Random().Next(0, cn.Length); + return Content($"随机字符:{cn[ch].ToString()}"); + } +``` +5. 定义一个字符串,字符串中有100个中文字符,需要从中随机取5-50个字符,组成新的字符 +```cs +public IActionResult Number5() + { + string cn = "阳光灿烂大地暖绿草如茵花娇艳山河壮丽美如画蓝天白云映水面飞鸟翱翔鱼欢跃春夏秋冬四季转日月星辰耀九天男女老少笑开颜手脚并用勤劳作口耳相传智慧添红黄蓝绿色彩艳大小高低各不同多少长短有分别快慢动静皆自然风雨雷电显神奇木石水火有妙用衣食住行不可少喜怒哀乐情万千诗词歌赋韵味长琴棋书画雅趣生仁义礼智信为本忠孝廉耻心中存团结友爱力量大互帮互助共前行努力奋斗创未来幸福生活乐无边"; + int len = new Random().Next(5, 51); + string str = new string(cn.OrderBy(x => new Random().Next()).Take(len).ToArray()); + + return Content($"随机字符:{str}"); + } +``` +6. 定义2个字符串,第一个字符串中放百家姓,第二个字符串中放中文字符,要求从第一个字符串随机取得一个姓,再从第二个字符串中随机获得1到2个字符组成新字符串,和第一个字符串取得的姓组成一个姓名 +```cs +public class Program +{ + private static readonly string[] Surnames = { "赵", "钱", "孙", "李", "周", "吴", "郑", "王", "冯", "陈" }; + private static readonly string[] ChineseCharacters = { "伟", "刚", "勇", "毅", "俊", "峰", "强", "军", "平", "保" }; + + public static void Main() + { + Random random = new Random(); + string surname = Surnames[random.Next(Surnames.Length)]; + int nameLength = random.Next(1, 3); + string name = string.Concat(Enumerable.Range(0, nameLength).Select(_ => ChineseCharacters[random.Next(ChineseCharacters.Length)])); + string fullName = surname + name; + + Console.WriteLine($"随机生成的姓名: {fullName}"); + } +} +``` +7. 利用以上方法,随机生成100个BlogCreateDto类型(有Title、Author、Content属性)的对象,其中的内容都是随机生成且长度不定,并将其渲染到视图 +```cs + public class BlogCreateDto +{ + public string Title { get; set; } + public string Author { get; set; } + public string Content { get; set; } +} + + private static readonly string[] Surnames = { "赵", "钱", "孙", "李", "周", "吴", "郑", "王", "冯", "陈" }; + private static readonly string[] ChineseCharacters = { "伟", "刚", "勇", "毅", "俊", "峰", "强", "军", "平", "保" }; + private static readonly string[] SampleTitles = { "如何学习编程", "ASP.NET MVC教程", "C#高级特性", "前端开发技巧", "数据库优化" }; + private static readonly string[] SampleContents = { "本文介绍了如何学习编程...", "ASP.NET MVC是一个强大的框架...", "C#语言有许多高级特性...", "前端开发需要掌握多种技术...", "数据库优化可以提高性能..." }; + private static readonly Random random = new Random(); + + public ActionResult Index() + { + List blogs = new List(); + for (int i = 0; i < 100; i++) + { + blogs.Add(GenerateRandomBlog()); + } + return View(blogs); + } + + private BlogCreateDto GenerateRandomBlog() + { + string surname = Surnames[Random.Next(Surnames.Length)]; + int nameLength = Random.Next(1, 3); + string name = string.Concat(Enumerable.Range(0, nameLength).Select(_ => ChineseCharacters[random.Next(ChineseCharacters.Length)])); + string fullName = surname + name; + + return new BlogCreateDto + { + Title = SampleTitles[Random.Next(SampleTitles.Length)], + Author = fullName, + Content = SampleContents[Random.Next(SampleContents.Length)] + }; + } +``` \ No newline at end of file diff --git "a/\347\256\241\346\230\237\350\210\252/20241122-\346\216\247\345\210\266\345\231\250\344\274\240\345\217\202.md" "b/\347\256\241\346\230\237\350\210\252/20241122-\346\216\247\345\210\266\345\231\250\344\274\240\345\217\202.md" new file mode 100644 index 0000000..71e6de7 --- /dev/null +++ "b/\347\256\241\346\230\237\350\210\252/20241122-\346\216\247\345\210\266\345\231\250\344\274\240\345\217\202.md" @@ -0,0 +1,88 @@ +# 笔记 +渲染视图 +```csharp +public IActionResult Index() +{ + return View(); +} +``` +# 控制器传参 + +1. 简单参数传递 在一个叫Blog控制器中,定义一个叫Index的Action,并且传递一个int类型的值,id为变量名 + +```cs +public IActionResult Index(int id) + { + return View(); + } +``` + +2. 简单参数传递 在一个叫Blog控制器中,定义一个叫Index_2的Action,并且传递一个string类型的值,id为变量名 + +```cs +public string Index_2(int id) + { + return id.ToString(); + } +``` + +3. 简单参数传递 在一个叫Blog控制器中,定义一个叫Index_3的Action,并且传递一个string类型的值,name为变量名 + +```cs +public IActionResult Index_3([FromBody]Student stu) + { + return Content(stu.Name); + } + public class Student + { + public string Name { get; set; } = null!; + } +``` + +4. 复杂参数传递 在一个叫Blog的控制器中,定义一个名为Create的Action,并且传递一个BlogCreateDto类型的值,blogCreateDto为变量名 + > PS BlogCreateDto类型具有Title、Author、Content自动属性 + > +```cs +public IActionResult Create([FromBody] BlogCreateDto blogCreateDto) + { + return View(); + } + public class BlogCreateDto + { + public string Title { get; set; } = null!; + public string Author { get; set; } = null!; + public string Content { get; set; } = null!; + } +``` + +5. 复杂参数传递 在一个叫Blog的控制器中,定义一个名为Create_1的Action,并且传递一个Products类型的值,productCreateDto为变量名 + > PS Products类型具有Name、Price、Stock自动属性 + > +```cs +public IActionResult Create([FromBody] Products productCreateDto) + { + return View(); + } + public class Products + { + public string Name { get; set; } = null!; + public string Price { get; set; } = null!; + public string Stock { get; set; } = null!; + } +``` + +6. 复杂参数传递 在一个叫Blog的控制器中,定义一个名为Create_2的Action,并且传递一个Students类型的值,studentCreateDto为变量名 + > PS Students类型具有StudentName、Sex、Age自动属性 + > +```cs +public IActionResult Create([FromBody] Students studentCreateDto) + { + return View(); + } + public class Students + { + public string StudentName { get; set; } = null!; + public string Sex { get; set; } = null!; + public string Age { get; set; } = null!; + } +``` -- Gitee