diff --git "a/\347\216\213\345\251\211\345\251\267/20241125-Action\347\232\204\350\277\224\345\233\236\345\200\274.md" "b/\347\216\213\345\251\211\345\251\267/20241125-Action\347\232\204\350\277\224\345\233\236\345\200\274.md" new file mode 100644 index 0000000000000000000000000000000000000000..48cadc121fb7cf0c69ea7968abb78b7576ebbfc3 --- /dev/null +++ "b/\347\216\213\345\251\211\345\251\267/20241125-Action\347\232\204\350\277\224\345\233\236\345\200\274.md" @@ -0,0 +1,170 @@ +## Action的返回值 +1. 基础数据练习类型:如List<>,int,string +- 基础数据类型int: +```cs +public int Index(){ + return 888; +} +``` +2. IActionResult,返回响应的状态码 +- 视图,重定向 + +- 重定向:设置重定向到哪个页面,就会一直跳到拿个页面 + - 如进入的网址是在list页面,跳的是index页面 + - BlogsController.cs页面 +```cs +using Microsoft.AspNetCore.Mvc; + +namespace Blog.Controllers; + +public class BlogsController : Controller +{ + + public IActionResult List() + { + return RedirectToAction("index"); + } + + public IActionResult Index() + { + return View(); + } +} +``` +3. ActionResult,前面两个(1,2)的结合体 +- 即可返回基础数据类型,也可以返回响应的状态码 + +```cs +using Microsoft.AspNetCore.Mvc; + +namespace Blog.Controllers; + +public class BlogsController : Controller +{ + + public IActionResult List() + { + return RedirectToAction("index"); + } + + public ActionResult Index() //序列化、反序列化 + { + var isOli = 1 == 3 ; + if(isOki) + { + return View(); + } + else + { + return true ; + } + } +} +``` +4. 内容响应:JsonResult, ContentResult + +5. POCO(比较老的类型对象)\ + + +```cs +using System; +using System.Collections.Generic; +using System.Linq; +using System.Web; +using System.Web.Mvc; +using System.IO; + +namespace MvcActionReturnType.Controllers +{ + public class HomeController : Controller + { + /// + /// 1、返回一个ViewResult对象 + /// + /// + public ActionResult Index() + { + return View(); + } + + /// + /// 2、返回一个json格式的数据 + /// + /// + public ActionResult Json() + { + var book = new { BookId = 1, BookName = "MVC框架" }; + return Json(book, JsonRequestBehavior.AllowGet); + } + + /// + /// 3、返回JavaScript + /// + /// + public ActionResult JavaScript() + { + string js = ""; + return JavaScript(js); + } + + /// + /// 4、返回FilePath + /// + /// + public ActionResult FilePath() + { + //return File("~/Content/校长 - 带你去旅行.mp3", "audio/mp3"); + return new FilePathResult("~/Content/校长 - 带你去旅行.mp3", "audio/mp3"); + } + + /// + /// 5、返回FileContent + /// + /// + public ActionResult FileContent() + { + string content = "Welcome To ASP.NET MVC"; + byte[] contents = System.Text.Encoding.UTF8.GetBytes(content); + return File(contents, "text/plain"); + } + + /// + /// 6、返回FileStream + /// + /// + public ActionResult FileStream() + { + string content = "Welcome To ASP.NET MVC"; + byte[] contents = System.Text.Encoding.UTF8.GetBytes(content); + FileStream fs = new FileStream(Server.MapPath("~/Content/2 开发环境下载安装说明.doc"), FileMode.Open); + return File(fs, "application/msword"); + } + + /// + /// 7、返回 ContentResult + /// + /// + public ActionResult ContentResult() + { + string content = "

Welcome To ASP.NET MVC

"; + return Content(content); + } + + + + public ActionResult About() + { + ViewBag.Message = "Your application description page."; + + return View(); + } + + public ActionResult Contact() + { + ViewBag.Message = "Your contact page."; + + return View(); + } + } +} +``` \ No newline at end of file diff --git "a/\347\216\213\345\251\211\345\251\267/20241127-\350\247\206\345\233\276Razor.md" "b/\347\216\213\345\251\211\345\251\267/20241127-\350\247\206\345\233\276Razor.md" new file mode 100644 index 0000000000000000000000000000000000000000..83d72bc0d75335ed00007863742141afa2e46140 --- /dev/null +++ "b/\347\216\213\345\251\211\345\251\267/20241127-\350\247\206\345\233\276Razor.md" @@ -0,0 +1,84 @@ +# 视图 +## 1.Razor 专业说法 模板引擎 + - 我们通过View函数,传入数据(ViewModel)给视图,这样一来,视图就可以通过@Model这个属性来取得这下数据 + - 实际,可以通过声明视图模型的类型,来获得自动提示或者自动感知 + +## 2.在视图上的几个用法 + - 在视图上定义简单的数据,在视图上显示(在视图上定义对象数据,在视图上定义集合数据,显示的问题) + - 后端传回来对象数据,在视图上显示 + - 在后端传回来集合数据,在视图上显示 + + + + ## - 在BlogsController.cs中 + ```cs + using Microsoft.AspNetCore.Mvc; + + namespave Blog.Controllers; + + public class BlogsController : Controller + { + public IActionResult List() + { + return RedirectToAction("index"); + + } + + // public dynamic Index() //序列化,反序列化 + // { + // return View(); + // } + + public dynamic Index() + { + var obj = new ProductsCreateDto + { + StudentName = "小李", + Age = "18", + Height = "178" + }; + var list = new List + { + new ProductsCreateDto + { + StudentName = "zhangsan", + Age = "15", + Height = "177" + }, + + new ProductsCreateDto + { + StudentName = "lisi", + Age = "17", + Height = "166" + }, + }; + return View(list); + } + } + ``` + + + + ## - 在Index.cshtml中 + - 记得@与字符串之间不要有空格 + ```cs + @{ + var str = "试试效果" + } + + @str + +

@str

+ @* @Model.StudentName + +

@Model.Age

+ + *@ + + @foreach(var item in @Model) + { +

@item.StudentName

+

@item.Age

+ } + ``` \ No newline at end of file diff --git "a/\347\216\213\345\251\211\345\251\267/20241128-\347\273\203\344\271\240.md" "b/\347\216\213\345\251\211\345\251\267/20241128-\347\273\203\344\271\240.md" new file mode 100644 index 0000000000000000000000000000000000000000..f47356a29ad8475945b354bf05dcfc957007f6cc --- /dev/null +++ "b/\347\216\213\345\251\211\345\251\267/20241128-\347\273\203\344\271\240.md" @@ -0,0 +1,59 @@ +## 6.定义2个字符串,第一个字符串中放百家姓,第二个字符串中放中文字符,要求从第一个字符串随机取得一个姓,再从第二个字符串中随机获得1到2个字符组成新字符串,和第一个字符串取得的姓组成一个姓名 + +```cs +using Microsoft.AspNetCore.Mvc; + +namespace Text.Controllers +{ + public class Name : Controller + { + + + // GET: Name/Generate + public IActionResult GetName() + { + // 定义一个包含百家姓的字符串 + string surnames = "赵钱孙李周吴郑王冯陈褚卫蒋沈韩杨朱秦尤许何吕施张孔曹严华金魏陶姜戚谢邹喻柏水窦章云苏潘葛奚范彭郎鲁韦昌马苗凤花方俞任袁柳酆鲍史唐费廉岑薛雷贺倪汤滕殷罗毕郝邬安常于时傅皮卞齐康伍余元卜顾孟平黄和穆萧尹姚邵湛汪祁毛禹狄米贝明臧计伏成戴谈宋茅庞熊纪舒屈项祝董梁杜阮蓝闵席季麻强贾路娄危江童颜郭梅盛林刁钟徐邱骆高夏蔡田樊胡凌霍虞万支柯昝管卢莫经房裘缪干解应宗丁宣贲邓郁单杭洪包诸左石崔吉钮龚程嵇邢滑裴陆荣翁荀羊於惠甄麴家封芮羿储靳汲邴糜松井段富巫乌焦巴弓牧隗山谷郏车侯宓蓬全郗班仰秋仲伊宫宁仇栾暴甘钭厉戎祖武符刘景詹束龙叶幸司韶郜黎蓟溥印宿白怀蒲邰从鄂索咸籍赖卓蔺屠蒙池乔阳郁胥能苍双闻莘党翟谭贡劳逄姬申扶堵冉宰郦雍却璩桑桂濮牛寿通边扈燕冀郏浦尚农温别庄晏柴瞿阎充慕连茹习宦艾鱼容向古易慎戈廖庾终暨居衡步都耿满弘匡国文寇广禄阙东国南辛阳佟海陆滕殷薛岳帅范崔程庹奚黎蓟薄后百里奚乜仰仉督归牟俟伽红复仉华督佴冒干邗戈乌仝佼佀儋丛仉昝仉殳偻付儇仉僖僭儆侃儋侨僳仉仝"; + + // 定义一个包含中文字符的字符串 + string Chinese = "协妮果国地浩高撒时来用们生到作们地于出就分对成会可主发年动同工也能三数切出现将两学自才了六用张行为把还认电新力能每取书车向行学军农史地场世进两全多工或保事产平已察和革位入今商所战与业到次专及件五结般代军具"; + + // 创建Random对象 + Random random = new Random(); + + // 随机选择一个姓 + int Index = random.Next(0, surnames.Length); + string Name = surnames.Substring(Index, 1); + + // 随机选择1到2个中文字符 + int Count = random.Next(1, 3); // 生成1到2之间的随机数 + string All = ""; + for (int i = 0; i < Count; i++) + { + int charIndex = random.Next(0, Chinese.Length); + All += Chinese.Substring(charIndex, 1); + } + + // 组成姓名 + string fullName = Name + All; + + // 将生成的姓名作为视图模型传递给视图 + ViewBag.FullName = fullName; + + // 返回视图 + return View(); + } + } +} + +``` + +```cshtml + +@{ + ViewBag.Title = "Generated Name"; +} + +

随机生成的姓名:

+

@ViewBag.FullName

+``` \ No newline at end of file