diff --git "a/\351\231\206\346\245\232\347\233\210/20241122\346\216\247\345\210\266\345\231\250\344\274\240\345\217\202.md" "b/\351\231\206\346\245\232\347\233\210/20241122\346\216\247\345\210\266\345\231\250\344\274\240\345\217\202.md" new file mode 100644 index 0000000000000000000000000000000000000000..90916b920cafe386cd8c7bfff0380510da8d3674 --- /dev/null +++ "b/\351\231\206\346\245\232\347\233\210/20241122\346\216\247\345\210\266\345\231\250\344\274\240\345\217\202.md" @@ -0,0 +1,122 @@ +# MVC练习 +## 第一题 +``` +dotnet new mvc +``` +## 第二题 +``` +dotnet new mvc -o Blog +``` +## 第三题 +``` +dotnet new mvc -o Blog +cd Blog +``` +## 第四题 +``` +dotnet new mvc -o 项目名称 +``` +## 第五题 +``` +dotnet new mvc -o Blog +cd Blog +``` +## 第六题 +``` +mkdir 解决方案名称 +cd 解决方案名称 + +dotnet new sln -n 解决方案文件名称 + +dotnet new mvc -n mvc项目 +dotnet sln add mvc项目 + +dotnet new classlib -n 综合项目1 +dotnet sln add 综合项目1 +dotnet new classlib -n 综合项目2 +dotnet sln add 综合项目2 +dotnet new classlib -n 综合项目3 +dotnet sln add 综合项目3 +``` +## 第七题 +``` + public IActionResult Ok() + { + return View(); + } +``` +## 第八题 +``` +public class BlogsController : Controller + { + public IActionResult Index() + { + return View(); + } + } + +``` +# 控制器传参 +## 1 +``` + public IActionResult Index(int Id) + { + return Content(Id.ToString) + } +``` +## 2 +``` + public IActionResult Index_2(string Id){ + return Content(Id) + } +``` +## 3 +``` + public IActionResult Index_2(string name){ + return Content(name) + } +``` +## 4 +``` + public class BlogContriller : Controller{ + [HttpPost] + public IActionResult Create([FromBody] BlogCreateDto blogCreateDto){ + return Content(blogCreateDto.Title) + } + } + public class BlogCreateDto{ + // Title等为自动属性 + public string Title{get;set;}=null!; + public string Author{get;set;}=null!; + public string Content{get;set;}=null!; + } + +``` +## 5 +``` + public class BlogContriller : Controller{ + [HttpPost] + public IActionResult Create([FromBody] Products productCreateDto){ + return Content(blogCreateDto.Title) + } + } + public class Products{ + public string Name{get;set;}=null!; + public string Price{get;set;}=null!; + public string Stock{get;set;}=null!; + } +``` +## 6 +``` + public class BlogContriller : Controller{ + [HttpPost] + public IActionResult Create_2([FromBody] Students studentCreateDto){ + return Content(blogCreateDto.Title) + } + } + public class Students{ + public string Name{get;set;}=null!; + public string Price{get;set;}=null!; + public string Stock{get;set;}=null!; + } +```