diff --git "a/\346\235\216\344\275\263\350\261\252/\347\254\224\350\256\260/1125.md" "b/\346\235\216\344\275\263\350\261\252/\347\254\224\350\256\260/1125.md" new file mode 100644 index 0000000000000000000000000000000000000000..d2fc621c281e5d402453293b7b1625c0ebd6a464 --- /dev/null +++ "b/\346\235\216\344\275\263\350\261\252/\347\254\224\350\256\260/1125.md" @@ -0,0 +1,165 @@ +### Action的返回值 +1. 基础数据练习类型:如List<>,int,string +- 基础数据类型int: +``` +public int Index(){ + return 888; +} +``` +2. IActionResult,返回响应的状态码 +- 视图,重定向 +- 重定向:设置重定向到哪个页面,就会一直跳到拿个页面 + - 如进入的网址是在list页面,跳的是index页面 + - BlogsController.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)的结合体 +- 即可返回基础数据类型,也可以返回响应的状态码 +``` +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(比较老的类型对象) +``` +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(); + } + } +} +``` diff --git "a/\346\235\216\344\275\263\350\261\252/\347\254\224\350\256\260/1127.md" "b/\346\235\216\344\275\263\350\261\252/\347\254\224\350\256\260/1127.md" new file mode 100644 index 0000000000000000000000000000000000000000..2bc1463fdadabd0bdf49e6767c18a1a7bf2f533c --- /dev/null +++ "b/\346\235\216\344\275\263\350\261\252/\347\254\224\350\256\260/1127.md" @@ -0,0 +1,8 @@ +## 视图 +1. Razor 专业说法 模版引擎 +- 大部分的时候,我们都是通过View函数,传入数据(ViewModel)给视图,这样一来,视图就可以通过@Model这个属性来取得这些数据 +- 实际上,我们可以通过声明视图模型的类型,来获得自动提示或者自动感知 +2. 在视图上显示数据的几个玩法 +- 在视图上定义简单数据,在视图上显示(在视图上定义对象数据,在视图上定义集合数据,显示的问题) +- 后端传回来对象的数据,在视图上显示 +- 在后端传回来集合数据,在视图上显示 \ No newline at end of file