diff --git "a/\351\237\251\345\276\267\347\276\216/20241119--1122\347\254\224\350\256\260\344\275\234\344\270\232.md" "b/\351\237\251\345\276\267\347\276\216/20241119--1122\347\254\224\350\256\260\344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..e6190f0a8c2ea5a29e4179c520f6a7976808bf2b --- /dev/null +++ "b/\351\237\251\345\276\267\347\276\216/20241119--1122\347\254\224\350\256\260\344\275\234\344\270\232.md" @@ -0,0 +1,353 @@ +# 11月19号到22号笔记 +dotnet new mvc -o .\名称\ +dotnet run --project .\名称\ +dotnet new sln -o .\名称\ +dotnet publish 将程序打包到服务器 +dotnet new mvc -o .\名称\ +简单参数的传递 +渲染数据到页面 +控制台返回类型,参数 +视图,重定向 +1. 一般数据类型int.double.string.IEnumberable等数据类型 +2. IActionResult类型:一个接口,用于返回HTTP状态信息,如200,401,404等端口 +3. 特定于格式的操作结果:如JsonResult和ContentResult + +# Linux练习 网站的搭建 +效果图如下: +![屏幕截图 2024-11-24 093602](https://hdm1ss.oss-cn-shenzhen.aliyuncs.com/屏幕截图 2024-11-24 093602.png) +# MVC练习1-10 +1. 第一题到第五: +dotnet new mvc -o .\Blog\ +dotnet run --project .\Blog\ +cd .\Blog\ +2. 第6题到第12题 +![屏幕截图 2024-11-23 231526](https://hdm1ss.oss-cn-shenzhen.aliyuncs.com/屏幕截图 2024-11-23 231526.png) +![屏幕截图 2024-11-23 231557](https://hdm1ss.oss-cn-shenzhen.aliyuncs.com/屏幕截图 2024-11-23 231557.png) +![屏幕截图 2024-11-23 231541](https://hdm1ss.oss-cn-shenzhen.aliyuncs.com/屏幕截图 2024-11-23 231541.png) +![屏幕截图 2024-11-23 231640](https://hdm1ss.oss-cn-shenzhen.aliyuncs.com/屏幕截图 2024-11-23 231640.png) +![屏幕截图 2024-11-23 231655](https://hdm1ss.oss-cn-shenzhen.aliyuncs.com/屏幕截图 2024-11-23 231655.png) +代码如下: +```html +using Microsoft.AspNetCore.Mvc; +namespace Blog.Controllers; + +public class BlogsController : Controller{ + //8 项目,创建一个新的控制器,名为Blogs,新的控制器拥有一个名为Index的Action, + // 该方法返回一个视图,视图显示“神级预判” + public IActionResult Index(){ + return View(); + } + // 9. + public IActionResult Music(){ + return View(); + } +// 10 + public IActionResult List(){ + return View(); + } +} + + +using Microsoft.AspNetCore.Mvc; +namespace Blog.Controllers; + +public class ProductsController : Controller{ + // 11.新增一个控制器,名为Products,该控制器具有一个名为Edit的Action, + // 这个Action接受一个int类型的参数id,显示这个id + public IActionResult Edit(int id){ + var aqString = "滴滴"; + var kaString = "嘻嘻"; + var result = ""; + if(id == 0){ + result=aqString; + } + else if(id == 1){ + result = kaString; + } + else{ + result="点点点"; + } + return Content(string.Format("{0},hello,Welcome",result)); + } +[HttpGet] + +// 12// 12在11题的新控制器中,新增一个名为Create的Action, +// 该Action接受一个类型为Students(有姓名、年龄、体长属性)的参数,并展示该参数的姓名属性 + public IActionResult Create(){ + var productStudent=new ProductStudent{ + Name="凯凯", + Age="18", + Tall="183" + }; + return View(productStudent); + } + +} +// 12在11题的新控制器中,新增一个名为Create的Action, +// 该Action接受一个类型为Students(有姓名、年龄、体长属性)的参数,并展示该参数的姓名属性 +public class ProductStudent{ + public string Name {get;set;}=null!; + public string Age {get;set;}=null!; + public string Tall {get;set;}=null!; + +} +views() +@model Blog.Controllers.ProductStudent; +@Model.Name +@Model.Age +@Model.Tall +``` +# 专项练习-控制器传参 +1. 第1题到第6题 +![屏幕截图 2024-11-23 231504](https://hdm1ss.oss-cn-shenzhen.aliyuncs.com/屏幕截图 2024-11-23 231504.png) +![屏幕截图 2024-11-23 231437](https://hdm1ss.oss-cn-shenzhen.aliyuncs.com/屏幕截图 2024-11-23 231437.png) +![屏幕截图 2024-11-23 231451](https://hdm1ss.oss-cn-shenzhen.aliyuncs.com/屏幕截图 2024-11-23 231451.png) +![屏幕截图 2024-11-23 231331](https://hdm1ss.oss-cn-shenzhen.aliyuncs.com/屏幕截图 2024-11-23 231331.png) +![屏幕截图 2024-11-23 231352](https://hdm1ss.oss-cn-shenzhen.aliyuncs.com/屏幕截图 2024-11-23 231352.png) +![屏幕截图 2024-11-23 231408](https://hdm1ss.oss-cn-shenzhen.aliyuncs.com/屏幕截图 2024-11-23 231408.png) +代码如下: +```html +using Microsoft.AspNetCore.Mvc; +namespace Blog.Controllers; + +public class BlogController : Controller{ + // 1.简单参数传递 在一个叫Blog控制器中,定义一个叫Index的Action, + // 并且传递一个int类型的值,id为变量名 + public dynamic Index(int id){ + return new {Id=id}; + } + // 2.简单参数传递 在一个叫Blog控制器中, + // 定义一个叫Index_2的Action,并且传递一个string类型的值,id为变量名 +public IActionResult Index_2(int id){ + var azString="karry"; + var zzString="apt"; + var res=""; + if(id == 0){ + res=azString; + } + else if(id == 1){ +res=zzString; + } + else{ + res="蠢蠢"; + } + return Content(string.Format("{0},hello",res)); +} +// 3.简单参数传递 在一个叫Blog控制器中, +// 定义一个叫Index_3的Action,并且传递一个string类型的值,name为变量名 +[HttpGet] +public Mypro Index_33(){ + var mypro=new Mypro{ + Name="karry" + }; + return mypro; +} +[HttpGet] +// 4.一个叫Blog的控制器中,定义一个名为Create的Action, +// 并且传递一个BlogCreateDto类型的值,blogCreateDto为变量名 +public BlogCreateDto Create(){ + var blog=new BlogCreateDto{ + Title="终于周末了", + Author="明天见", + Content="再见再见" + }; + return blog; +} +// 5.复杂参数传递 在一个叫Blog的控制器中,定义一个名为Create_1的Action, +// 并且传递一个Products类型的值,productCreateDto为变量名 +public Products Create_1(){ + var productCreateDto=new Products{ + Names="笑笑", + Price="1000", + Stock="眼镜" + }; + return productCreateDto; +} +// 6.复杂参数传递 在一个叫Blog的控制器中,定义一个名为Create_2的Action, +// 并且传递一个Students类型的值,studentCreateDto为变量名 +public Students Create_2(){ + var studentCreateDto=new Students{ + StudentName="媛媛", + Sex="女", + Age="18" + }; + return studentCreateDto; +} +} + +public class Mypro{ + public string Name{get;set;}=null!; +} + +// 4. +public class BlogCreateDto{ + public string Title{get;set;}=null!; + public string Author{get;set;}=null!; + public string Content{get;set;}=null!; + +} +// 5. +public class Products{ + public string Names{get;set;}=null!; + public string Price{get;set;}=null!; + public string Stock{get;set;}=null!; +} + +// 6. +public class Students{ + public string StudentName{get;set;}=null!; + public string Sex{get;set;}=null!; + public string Age{get;set;}=null!; +} + +views() +@model Blog.Controllers.BlogCreateDto; +@Model.Title; +@Model.Author; +@Model.Content +``` +# 专项练习-基础能力随机数 +1. 第1题到7题 +![屏幕截图 2024-11-23 231732](https://hdm1ss.oss-cn-shenzhen.aliyuncs.com/屏幕截图 2024-11-23 231732.png) +![屏幕截图 2024-11-23 231802](https://hdm1ss.oss-cn-shenzhen.aliyuncs.com/屏幕截图 2024-11-23 231802.png) +![屏幕截图 2024-11-23 231748](https://hdm1ss.oss-cn-shenzhen.aliyuncs.com/屏幕截图 2024-11-23 231748.png) +代码如下: +```html +using Microsoft.AspNetCore.Mvc; +namespace Blog.Controllers; + +public class RandController : Controller{ +// 1.生成一个随机整数,范围[0,100],注意是否包含 +public IActionResult Index(){ + RandomNumber model=new RandomNumber(); + Random random=new Random(); + model.Randsnumber=random.Next(0,101); + return View(model); + +} +// 2.生成一个随机整数,范围(0,100],注意是否包含 +public IActionResult Itwo(){ + RandTwo model=new RandTwo(); + Random rand=new Random(); + model.Randstwos=rand.Next(0,100); + return View(model); +} +//3.生成10个随机整数,范围[5,80],注意是否包含 +public IActionResult Ithree(){ + RandThree model=new RandThree(); + Random rands=new Random(); + model.Randsthrees=rands.Next(5,80); + return View(model); +} +// 4.定义一个字符串,字符串中有100个中文字符,需要从中随机取1个字符串 +public char GetRandomChineseCharacter() +{ + string chineseCharacters = "的一是在不了有和人这中大为上个国我以要他时来用们生到作地于出就分对成会可主发年动同工也能下过子说产种面而方后多定行学法所民得经十三之进样理心体信息东件感理"; + Random random = new Random(); + return chineseCharacters[random.Next(chineseCharacters.Length)]; +} + +// 5定义一个字符串,字符串中有100个中文字符,需要从中随机取5-50个字符,组成新的字符 +public string GetRandomChineseSubstring() +{ + string chineseCharacters = "的一是在不了有和人这中大为上个国我以要他时来用们生到作地于出就分对成会可主发年动同工也能下过子说产种面而方后多定行学法所民得经十三之进样理心体信息东件感理"; + Random random = new Random(); + int length = random.Next(5, 51); // 5到50个字符 + return new string(chineseCharacters.Take(length).ToArray()); +} +// 6. + +} +// 1. +public class RandomNumber +{ + public int Randsnumber{get;set;} +} +// 2. +public class RandTwo{ + public int Randstwos{get;set;} +} +// 3. +public class RandThree{ + public int Randsthrees{get;set;} +} +// 4. +public class RandFour{ + public string Randfours{get;set;}=null!; +} + +``` + +# 专项练习-控制器返回值渲染数据 简单,复杂,集合渲染 +![屏幕截图 2024-11-23 231927](https://hdm1ss.oss-cn-shenzhen.aliyuncs.com/屏幕截图 2024-11-23 231927.png) +代码如下: +```html +using Microsoft.AspNetCore.Mvc; +namespace Blog.Controllers; + +public class SimpleController : Controller{ +//1.2.渲染简单数据,复杂数据到页面 +[HttpGet] +public IActionResult Editone(int id){ +// var simone=new Simone{ +// var str=new List{ +// "看", "所", "用", "基", "千", "坚", "离", "照", "硬", "金", "认", "性", "要", "证", "别", "息", "华", "导", "结", "安", "立", "历", "广", "即", "进", "老", "百", "动", "觉", "八", "确", "研", "前", "黄", "认", "华", "教", "武", "法", "北", "装", "装", "历", "须", "年", "路", "形", "想", "际", "专", "化", "亚", "山", "春", "三", "控", "能", "压", "名", "为", "兴", "高", "争", "断", "通", "何", "对", "行", "导", "星", "飞", "国", "飞", "受", "步", "于", "作", "管", "应", "习", "就", "历", "河", "界", "了", "异", "爱", "走", "变", "联", "十", "界", "招", " ", "分", "共", "引", "正", "服", "解" + +// }; +var list=new List{ +new(){ + Title="想见你", + Author="小凯", + Content="明天见", + Name="karry" +}, +new(){ + Title="想见你想见", + Author="小凯", + Content="明天见", + Name="karry" +}, +new(){ + Title="想见你", + Author="小凯", + Content="明年会见", + Name="karry,karry" +}, + }; + return View(list); + +} +} +// 1. +public class Simone{ + public string Title{get;set;}=null!; + public string Author{get;set;}=null!; + public string Content{get;set;}=null!; + public string Name{get;set;}=null!; +} +@model List; + @* @model Blog.Controllers.Simone; + @Model.Author; + @Model.Title *@ + + + + + + + + + @foreach(var item in @Model){ + + + + + + + + } +
标题作者内容姓名操作
@item.Title@item.Author@item.Content@item.Name + + +
+``` \ No newline at end of file diff --git "a/\351\237\251\345\276\267\347\276\216/images/desktop.ini" "b/\351\237\251\345\276\267\347\276\216/images/desktop.ini" new file mode 100644 index 0000000000000000000000000000000000000000..37432b53e12a2e175ab84148fc9fd0a8f03d149c --- /dev/null +++ "b/\351\237\251\345\276\267\347\276\216/images/desktop.ini" @@ -0,0 +1,3 @@ +[LocalizedFileNames] +Ļͼ 2024-11-24 142246.png=@Ļͼ 2024-11-24 142246.png,0 +Ļͼ 2024-11-24 093602.png=@Ļͼ 2024-11-24 093602.png,0 diff --git "a/\351\237\251\345\276\267\347\276\216/images/\345\261\217\345\271\225\346\210\252\345\233\276 2024-11-24 093602.png" "b/\351\237\251\345\276\267\347\276\216/images/\345\261\217\345\271\225\346\210\252\345\233\276 2024-11-24 093602.png" new file mode 100644 index 0000000000000000000000000000000000000000..da4a75d62d87f4bdcc4c928020fccf65996be4ff Binary files /dev/null and "b/\351\237\251\345\276\267\347\276\216/images/\345\261\217\345\271\225\346\210\252\345\233\276 2024-11-24 093602.png" differ diff --git "a/\351\237\251\345\276\267\347\276\216/images/\345\261\217\345\271\225\346\210\252\345\233\276 2024-11-24 142246.png" "b/\351\237\251\345\276\267\347\276\216/images/\345\261\217\345\271\225\346\210\252\345\233\276 2024-11-24 142246.png" new file mode 100644 index 0000000000000000000000000000000000000000..4bcbd039aa1706d3caca96adf154346541f67a37 Binary files /dev/null and "b/\351\237\251\345\276\267\347\276\216/images/\345\261\217\345\271\225\346\210\252\345\233\276 2024-11-24 142246.png" differ