From fabb1f24c6ff33b099945688e0ed86b141473369 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A1=B9=E5=AD=99=E6=9E=AB?= <3268254649@qq.com> Date: Sun, 24 Nov 2024 20:26:55 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...345\222\214mvc\347\273\203\344\271\240.md" | 89 +++++++++++++++ ...50\345\222\214\347\254\224\350\256\260.md" | 25 +++++ ...40\345\217\202\347\254\224\350\256\260.md" | 47 ++++++++ ...40\345\217\202\347\273\203\344\271\240.md" | 80 ++++++++++++++ ...40\345\217\202\347\254\224\350\256\260.md" | 38 +++++++ ...36\345\200\274\347\273\203\344\271\240.md" | 103 ++++++++++++++++++ 6 files changed, 382 insertions(+) create mode 100644 "\351\241\271\345\255\231\346\236\253/20241119lunnix\345\222\214mvc\347\273\203\344\271\240.md" create mode 100644 "\351\241\271\345\255\231\346\236\253/20241119\346\216\247\345\210\266\345\231\250\345\222\214\347\254\224\350\256\260.md" create mode 100644 "\351\241\271\345\255\231\346\236\253/20241121\346\216\247\345\210\266\344\274\240\345\217\202\347\254\224\350\256\260.md" create mode 100644 "\351\241\271\345\255\231\346\236\253/20241121\346\216\247\345\210\266\345\231\250\344\274\240\345\217\202\347\273\203\344\271\240.md" create mode 100644 "\351\241\271\345\255\231\346\236\253/20241122\344\274\240\345\217\202\347\254\224\350\256\260.md" create mode 100644 "\351\241\271\345\255\231\346\236\253/20241122\346\216\247\345\210\266\345\231\250\350\277\224\345\233\236\345\200\274\347\273\203\344\271\240.md" diff --git "a/\351\241\271\345\255\231\346\236\253/20241119lunnix\345\222\214mvc\347\273\203\344\271\240.md" "b/\351\241\271\345\255\231\346\236\253/20241119lunnix\345\222\214mvc\347\273\203\344\271\240.md" new file mode 100644 index 0000000..9745c00 --- /dev/null +++ "b/\351\241\271\345\255\231\346\236\253/20241119lunnix\345\222\214mvc\347\273\203\344\271\240.md" @@ -0,0 +1,89 @@ +# 练习 +# 效果图 + + + +# MVC练习题 +## 、创建一个控制台项目 +dotnet new console + +## 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 BlogDir + +## 6、创建一个带解决方案,其下有一个MVC项目,3个类库项目的“综合项目” +dotnet new sln -n Solution + +MVC项目: dotnet new mvc -n Blog + +类库: dotnet new classlib -n ClassLib1 dotnet new classlib -n ClassLib2 dotnet new classlib -n ClassLib3 + +## 7、创建一个项目,在默认控制器(Home)下,新增一个Action方法,名为Ok,同时为其创建对应视图以显示这个视图 +创建项目 dotnet new mvc -n Blog + + public IActionResult Ok() + { + return View(); + } + +创建对应视图 Views/Home/Ok.cshtml + +## 8、创建一个项目,创建一个新的控制器,名为Blogs,新的控制器拥有一个名为Index的Action,该方法返回一个视图,视图显示“神级预判” + public class BlogsController : Controller + { + public IActionResult Index() + { + return View(); + } + } +创建对应视图Views/Blogs/Index.cshtml + +

神级预判

+ 9、给第8题的新控制,添加一个新的Action,名为Music,不接受任何参数,并返回对应的视图,视图显示“顶级打野” + public IActionResult Music() + { + return View(); + } +创建对应视图Views/Blogs/Music.cshtml + +顶级打野 +10、给第8题的新控制器,新增一个Action,名为List,不接受任何参数,并返回对应视图,视图显示一个经典CRUD界面 + public IActionResult List() + { + return View(); + } +创建对应视图Views/Blogs/List.cshtml + +# 11、新增一个控制器,名为Products,该控制器具有一个名为Edit的Action,这个Action接受一个int类型的参数id,显示这个id +public class ProductsController : Controller +{ + public IActionResult Edit(int id) + { + ViewBag.Id = id; + return View(); + } +} +创建对应视图Views/Products/Edit.cshtml + +

Edit Product

+

编辑的产品ID:@ViewBag.Id

+ +# 12.在11题的新控制器中,新增一个名为Create的Action,该Action接受一个类型为Students(有姓名、年龄、体长属性)的参数,并展示该参数的姓名属性 +public class Main { + public static void createAction(Students student) { + System.out.println("姓名: " + student.getName()); + } + + public static void main(String[] args) { + Students student = new Students("风", 20, 175); + createAction(student); + } +} \ No newline at end of file diff --git "a/\351\241\271\345\255\231\346\236\253/20241119\346\216\247\345\210\266\345\231\250\345\222\214\347\254\224\350\256\260.md" "b/\351\241\271\345\255\231\346\236\253/20241119\346\216\247\345\210\266\345\231\250\345\222\214\347\254\224\350\256\260.md" new file mode 100644 index 0000000..370d229 --- /dev/null +++ "b/\351\241\271\345\255\231\346\236\253/20241119\346\216\247\345\210\266\345\231\250\345\222\214\347\254\224\350\256\260.md" @@ -0,0 +1,25 @@ +## 1、登陆服务器 + - ssh root@域名 +## 2、安装nginx apt install -y ninx +## 3、测试 systemctl status nginx +## 4、安装sdk +(官网下载8.0版本) 更新软件源 apt update + +## 创建一个新的文件夹 +命令:mkdir [文件夹名] + +查看当前目录下的文件和文件夹 +命令:ls +打包程序 +dotnet publish + +## 配置 Nginx 反向代理 +server { + listen 80; + server_name 域名或者IP; + + location / { + proxy_pass http://localhost:5000; + } +} +检查是否有语法错误:nginx -t 重新加载配置:nginx -s reload \ No newline at end of file diff --git "a/\351\241\271\345\255\231\346\236\253/20241121\346\216\247\345\210\266\344\274\240\345\217\202\347\254\224\350\256\260.md" "b/\351\241\271\345\255\231\346\236\253/20241121\346\216\247\345\210\266\344\274\240\345\217\202\347\254\224\350\256\260.md" new file mode 100644 index 0000000..541a537 --- /dev/null +++ "b/\351\241\271\345\255\231\346\236\253/20241121\346\216\247\345\210\266\344\274\240\345\217\202\347\254\224\350\256\260.md" @@ -0,0 +1,47 @@ +通过路由传递参数: +在控制器的Action中,你可以通过在路由模板中定义参数来接收参数。 + +// GET: Home/Details/5 +public IActionResult Details(int id) +{ + var item = _context.Items.FirstOrDefault(m => m.Id == id); + return View(item); +} +通过表单传递参数: +当使用表单提交数据时,可以通过模型绑定将表单字段绑定到Action方法的参数。 + +public class Item +{ + public int Id { get; set; } + public string Name { get; set; } +} + +// POST: Home/Create +[HttpPost] +public IActionResult Create(Item item) +{ + // 保存item对象到数据库 +} +通过FromBody传递参数: +对于使用HTTP POST或PUT方法提交的JSON或XML数据,可以使用[FromBody]属性来接收数据。 + +[HttpPost] +public IActionResult Create([FromBody] Item item) +{ + // 保存item对象到数据库 +} +通过查询字符串传递参数: +当参数通过URL的查询字符串传递时,你可以在Action方法中通过参数接收它们。 + +// GET: Home/Details?name=John&age=30 +public IActionResult Details(string name, int age) +{ + // 使用name和age参数 +} +通过Header传递参数: +可以通过[FromHeader]属性来获取HTTP请求头中的参数。 + +public IActionResult GetSecretData([FromHeader] string secretKey) +{ + // 根据secretKey参数执行操作 +} \ No newline at end of file diff --git "a/\351\241\271\345\255\231\346\236\253/20241121\346\216\247\345\210\266\345\231\250\344\274\240\345\217\202\347\273\203\344\271\240.md" "b/\351\241\271\345\255\231\346\236\253/20241121\346\216\247\345\210\266\345\231\250\344\274\240\345\217\202\347\273\203\344\271\240.md" new file mode 100644 index 0000000..a9bb4d9 --- /dev/null +++ "b/\351\241\271\345\255\231\346\236\253/20241121\346\216\247\345\210\266\345\231\250\344\274\240\345\217\202\347\273\203\344\271\240.md" @@ -0,0 +1,80 @@ +专项练习-控制器传参 +# 1、简单参数传递 在一个叫Blog控制器中,定义一个叫Index的Action,并且传递一个int类型的值,id为变量名 + public class BlogController : Controller + { + public IActionResult Index(int id) + { + return Content(id.ToString) + } + + } +# 2、简单参数传递 在一个叫Blog控制器中,定义一个叫Index_2的Action,并且传递一个string类型的值,id为变量名 +public class BlogController : Controller + { + + public IActionResult Index_2(string id) + { + return Content(id) + } + + } +# 3、简单参数传递 在一个叫Blog控制器中,定义一个叫Index_3的Action,并且传递一个string类型的值,name为变量名 +public class BlogController : Controller + { + public IActionResult Index_3(string name) + { + return Content(name) + } + } +# 4、复杂参数传递 在一个叫Blog的控制器中,定义一个名为Create的Action,并且传递一个BlogCreateDto类型的值,blogCreateDto为变量名 +PS BlogCreateDto类型具有Title、Author、Content自动属性 + +public class BlogController : Controller + { + [HttpPost] + public IActionResult Create([FromBody] BlogCreateDto blogCreateDto){ + + return Content(blogCreateDto.Title) + } + + public class BlogCreateDto{ + + //BlogCreateDto类型具有Title、Author、Content自动属性 + 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自动属性 + + public class BlogController : Controller + { + [HttpPost] + public IActionResult Create([FromBody] Products productCreateDto){ + return Content(blogCreateDto.Title) + } + //PS Products类型具有Name、Price、Stock自动属性 + 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自动属性 + + public class BlogController : Controller + { + 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!; + } + } \ No newline at end of file diff --git "a/\351\241\271\345\255\231\346\236\253/20241122\344\274\240\345\217\202\347\254\224\350\256\260.md" "b/\351\241\271\345\255\231\346\236\253/20241122\344\274\240\345\217\202\347\254\224\350\256\260.md" new file mode 100644 index 0000000..13d0ca4 --- /dev/null +++ "b/\351\241\271\345\255\231\346\236\253/20241122\344\274\240\345\217\202\347\254\224\350\256\260.md" @@ -0,0 +1,38 @@ +控制器返回类型 +一般数据类型直接返回 如int、double、string、IEnumerable等数据类型 +IActionResult 一个接口,用于返回HTTP信息状态,如200、401、404等 +视图 +重定向 +ActionResult类型 将一般数据类型和HTTP状态信息混合使用 +特定于格式的操作结果:如JsonResult和ContentResult +POCO(普通旧CLR对象) +在ASP.NET Core MVC中,控制器的Action方法可以返回多种类型的值,以下是一些常见的返回类型: +IActionResult 或 ActionResult: +这是最常用的返回类型,允许你返回不同类型的HTTP响应。例如,可以返回ViewResult、RedirectResult、JsonResult等。 + +public IActionResult Index() +{ + return View(); +} +ViewResult: +返回一个视图,通常用于渲染HTML页面。 + +public IActionResult Index() +{ + return View(); +} +JsonResult: +返回一个包含JSON数据的HTTP响应。 + +public IActionResult GetJson() +{ + var data = new { Name = "张三" }; + return Json(data); +} +ContentResult: +返回一个包含纯文本内容的HTTP响应 + +public IActionResult GetText() +{ + return Content("Hello, World!", "text/plain"); +} \ No newline at end of file diff --git "a/\351\241\271\345\255\231\346\236\253/20241122\346\216\247\345\210\266\345\231\250\350\277\224\345\233\236\345\200\274\347\273\203\344\271\240.md" "b/\351\241\271\345\255\231\346\236\253/20241122\346\216\247\345\210\266\345\231\250\350\277\224\345\233\236\345\200\274\347\273\203\344\271\240.md" new file mode 100644 index 0000000..eaefd80 --- /dev/null +++ "b/\351\241\271\345\255\231\346\236\253/20241122\346\216\247\345\210\266\345\231\250\350\277\224\345\233\236\345\200\274\347\273\203\344\271\240.md" @@ -0,0 +1,103 @@ +渲染简单数据到页面 + public class EndController : Controller + { + [HttpGet] + public IActionResult Editone(int id) +{ + var list=new List{ + new(){ + Title="007", + Author="张三", + Content="听 海哭的声音", + Name="Jackson" + }, + new(){ + Title="007", + Author="张三", + Content="听 海哭的声音", + Name="Jackson" + }, + new(){ + Title="007", + Author="张三", + Content="听 海哭的声音", + Name="Jackson" + }, + new(){ + Title="007", + Author="张三", + Content="听 海哭的声音", + Name="Jackson" + }, + new(){ + Title="007", + Author="张三", + Content="听 海哭的声音", + Name="Jackson" + }, + new(){ + Title="007", + Author="张三", + Content="听 海哭的声音", + Name="Jackson" + } + }; + return View(list); +} + + } +public class SimpleViewModel +{ + 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 + + + + + + + + + + @foreach(var item in @Model){ + + + + + + + + } +
标题作者内容姓名操作
@item.Title@item.Author@item.Content@item.Name + + +
+ + ## 2.页面出不来 一直报错 + + ## 3.渲染集合到页面 + public IActionResult CollectionView() +{ + CollectionViewModel model = new CollectionViewModel + { + Items = new List { "Item1", "Item2", "Item3" } + }; + return View(model); +} + +@model YourNamespace.CollectionViewModel + +
    + @foreach (var item in Model.Items) + { +
  • @item
  • + } +
\ No newline at end of file -- Gitee