diff --git "a/\351\273\216\346\254\243\346\254\243/20241120--Debian\351\203\250\347\275\262MVC\347\232\204\346\235\241\344\273\266\347\254\224\350\256\260.md" "b/\351\273\216\346\254\243\346\254\243/20241120--Debian\351\203\250\347\275\262MVC\347\232\204\346\235\241\344\273\266\347\254\224\350\256\260.md" index 620eb3b97558da129d6ae12a40127396daf34a6e..6d828d5015b3e8b7e68b94ec6541fecefcc74575 100644 --- "a/\351\273\216\346\254\243\346\254\243/20241120--Debian\351\203\250\347\275\262MVC\347\232\204\346\235\241\344\273\266\347\254\224\350\256\260.md" +++ "b/\351\273\216\346\254\243\346\254\243/20241120--Debian\351\203\250\347\275\262MVC\347\232\204\346\235\241\344\273\266\347\254\224\350\256\260.md" @@ -1,3 +1,5 @@ + +# 笔记 一、在服务器上安装了运行环境或者是调试环境(必要条件) 1、如何安装SDK包管理器 - 先下载个文件 wget curl diff --git "a/\351\273\216\346\254\243\346\254\243/20241122--mvc\350\247\206\345\233\276\345\217\202\346\225\260\347\254\224\350\256\260.md" "b/\351\273\216\346\254\243\346\254\243/20241122--mvc\350\247\206\345\233\276\345\217\202\346\225\260\347\254\224\350\256\260.md" index 4c33dfa5e20e4835aff1ef744f7b65db8e6f2bc8..cf1947f81bba61be33519c3d93cc0ae8d7ac99b6 100644 --- "a/\351\273\216\346\254\243\346\254\243/20241122--mvc\350\247\206\345\233\276\345\217\202\346\225\260\347\254\224\350\256\260.md" +++ "b/\351\273\216\346\254\243\346\254\243/20241122--mvc\350\247\206\345\233\276\345\217\202\346\225\260\347\254\224\350\256\260.md" @@ -1,3 +1,171 @@ +# 作业 +## 主要代码 +### 项目结构与mvc +```cs +//1、 +dotnet new console -n Blog +//2、 +dotnet new console -o Blog +//3、 +dotnet new mvc -n BlogMVC +//4、 +dotnet new mvc -o BlogMVC +//5、 +//使用命令行工具,运行以下命令创建解决方案: +dotnet new sln -n ComprehensiveSolution + +//创建MVC项目: +dotnet new mvc -n BlogMVC + +//创建三个类库项目: +dotnet new classlib -n ClassLibrary1 +dotnet new classlib -n ClassLibrary2 +dotnet new classlib -n ClassLibrary3 + +//将所有项目添加到解决方案中: +dotnet sln add BlogMVC/BlogMVC.csproj +dotnet sln add ClassLibrary1/ClassLibrary1.csproj +dotnet sln add ClassLibrary2/ClassLibrary2.csproj +dotnet sln add ClassLibrary3/ClassLibrary3.csproj +//6、创建一个项目,在默认控制器(Home)下,新增一个Action方法,名为Ok,同时为其创建对应视图以显示这个视图 +//在Controllers文件夹下的HomeController.cs中添加以下方法: +public IActionResult Ok() +{ + return View(); +} +//创建视图文件,在Views/Home文件夹下创建Ok.cshtml文件,并添加以下内容: +
This is the Ok view.
+ +//7. 创建一个项目,创建一个新的控制器,名为Blogs,新的控制器拥有一个名为Index的Action,该方法返回一个视图,视图显示“神级预判” +//在Controllers文件夹下创建一个新的控制器BlogsController.cs,并添加以下内容: +public class BlogsController : Controller +{ + public IActionResult Index() + { + return View(); + } +} +//创建视图文件,在Views/Blogs文件夹下创建Index.cshtml文件,并添加以下内容: +神级预判
+ +//8. 给第7题的新控制器,添加一个新的Action,名为Music,不接受任何参数,并返回对应的视图,视图显示“顶级打野” +//在BlogsController.cs中添加以下方法: +public IActionResult Music() +{ + return View(); +} +//创建视图文件,在Views/Blogs文件夹下创建Music.cshtml文件,并添加以下内容: +顶级打野
+//9. 给第7题的新控制器,新增一个Action,名为List,不接受任何参数,并返回对应视图,视图显示一个经典CRUD界面 +//在BlogsController.cs中添加以下方法: +public IActionResult List() +{ + return View(); +} +//创建视图文件,在Views/Blogs文件夹下创建List.cshtml文件,并添加以下HTML代码来模拟CRUD界面: +这里是CRUD界面
+//10. 新增一个控制器,名为Products,该控制器具有一个名为Edit的Action,这个Action接受一个int类型的参数id,显示这个id +//在Controllers文件夹下创建一个新的控制器ProductsController.cs,并添加以下内容: +public class ProductsController : Controller +{ + public IActionResult Edit(int id) + { + return View(id); + } +} +//创建视图文件,在Views/Products文件夹下创建Edit.cshtml文件,并添加以下内容: +ID: @Model
+//在10题的新控制器中,新增一个名为Create的Action,该Action接受一个类型为Students的参数,并展示该参数的姓名属性 +//首先,需要定义Student模型。在项目中创建一个新的文件夹Models,并在其中创建Student.cs文件,内容如下: +namespace YourProjectName.Models +{ + public class Student + { + public string Name { get; set; } + public int Age { get; set; } + public double Height { get; set; } + } +} +//在ProductsController.cs中添加以下方法: +public IActionResult Create(Student student) +{ + return View(student); +} +//创建视图文件,在Views/Products文件夹下创建Create.cshtml文件,并添加以下内容: +Name: @Model.Name
+``` +### 传参 +```cs +using Microsoft.AspNetCore.Mvc; + +namespace Blog.Collections +{ + public class BlogController : Controller + { + // 简单传参 + public IActionResult Index(int id) + { + return Content(id.ToString()); + var blogCreateDto=new BlogCreateDto{ + Title="点击", + Author="可可", + Content="阿斯蒂芬巴萨的语法可是对于" + }; + var productCreateDto=new Products{ + Name="可可", + Price="33", + Stock="22d" + }; + // return View(blogCreateDto); + return View(productCreateDto); + } + + public IActionResult Index_2(string id) + { + return Content(id.ToString()); + } + + public IActionResult Index_3(string name) + { + return Content(name.ToString()); + } + + + +[HttpPost] + public IActionResult Create([FromBody] BlogCreateDto blogCreateDto){ + // return Content(JsonSerializer.Serialize(blogCreateDto)); + return View(blogCreateDto); + } + [HttpPost] + public IActionResult Create_1([FromBody] Products productCreateDto){ + // return Content(JsonSerializer.Serialize(productCreateDto)); + return View(productCreateDto); + } + [HttpPost] + public IActionResult Create_2([FromBody]Students studentCreateDto){ + //return Content(JsonSerializer.Serialize(studentCreateDto)); + return View(studentCreateDto); + } +} + + +} +public class Students{ + public string Name{get;set;}=null!; + public string Age{get;set;}=null!; + public string Weight{get;set;}=null!; + public string Height{get;set;}=null!; +} +``` + +# 笔记 在ASP.NET Core MVC中,IActionResult是一个接口,它定义了控制器动作方法可能返回的结果类型。 IActionResult接口本身不包含任何参数,但是它的实现类通常包含参数,这些参数用于指定如何生成HTTP响应。 diff --git "a/\351\273\216\346\254\243\346\254\243/20241125--Action\350\277\224\345\233\236\345\200\274\347\261\273\345\236\213.md" "b/\351\273\216\346\254\243\346\254\243/20241125--Action\350\277\224\345\233\236\345\200\274\347\261\273\345\236\213.md" new file mode 100644 index 0000000000000000000000000000000000000000..e88c435df3fa035ab745c83d4234732c1fe24a86 --- /dev/null +++ "b/\351\273\216\346\254\243\346\254\243/20241125--Action\350\277\224\345\233\236\345\200\274\347\261\273\345\236\213.md" @@ -0,0 +1,199 @@ +# 作业 +## 主要代码 +```cs +using Microsoft.AspNetCore.Mvc; +namespace Blog.Collections +{ + public class HomeController : Controller +{ public IActionResult Index(){ + // 1.生成随机数 + Random random=new Random(); + // 2.范围[0,100] + int num=random.Next(0,101); + return View(num); + } + public IActionResult Create(){ + // 1.生成随机数 + Random random=new Random(); + // 2.范围(0,100] + int num=random.Next(1,101); + return View(num); + } + public IActionResult Baby(){ + // 1.生成随机数 + Random random=new Random(); + // 2.定一个列表 类型为int + var num=new List@Model.Key
+ + +``` + +## 详细步骤 + +### 1. 创建控制器 +- 在Visual Studio中,右键点击`Controllers`文件夹,选择`Add` -> `Controller...`。 +- 选择`MVC 5 Controller - Empty`模板。 +- 命名控制器,并确保继承自`Controller`类。 + +### 2. 创建模型 +- 在`Models`文件夹下创建一个新的C#类文件。 +- 定义与数据库表对应的属性和方法。 + +### 3. 创建视图 +- 右键点击控制器中的操作方法,选择`Add View...`。 +- 选择一个适当的布局页,添加模型类(如果需要)。 +- 使用Razor语法`@Model.PropertyName`来绑定模型数据。 + +## 注意事项 + +- 确保控制器、模型和视图之间的数据传递正确无误。 +- 使用Razor语法在视图中绑定模型数据。 +- 遵循MVC框架的约定,如文件夹结构、命名约定等。 +- 确保视图能够正确解析模型数据,特别是当使用强类型视图时。 +``` + diff --git "a/\351\273\216\346\254\243\346\254\243/20241129--\344\275\277\347\224\250\350\247\206\345\233\276\343\200\201\346\216\247\345\210\266\345\231\250\343\200\201\346\250\241\345\236\213\346\250\241\346\213\237\346\225\260\346\215\256\345\272\223.md" "b/\351\273\216\346\254\243\346\254\243/20241129--\344\275\277\347\224\250\350\247\206\345\233\276\343\200\201\346\216\247\345\210\266\345\231\250\343\200\201\346\250\241\345\236\213\346\250\241\346\213\237\346\225\260\346\215\256\345\272\223.md" new file mode 100644 index 0000000000000000000000000000000000000000..67c037d7125db8e3b6f437fd2836b08a6a7ef6bb --- /dev/null +++ "b/\351\273\216\346\254\243\346\254\243/20241129--\344\275\277\347\224\250\350\247\206\345\233\276\343\200\201\346\216\247\345\210\266\345\231\250\343\200\201\346\250\241\345\236\213\346\250\241\346\213\237\346\225\260\346\215\256\345\272\223.md" @@ -0,0 +1,235 @@ +# 作业 +## 效果图 + +## 主要代码 +```cs + //控制器 + using Microsoft.AspNetCore.Mvc; +using Blog.Controllers; +using Blog.Models; + +public class BlogsController: Controller{ + public IActionResult Index() + { + return View(Db.Blogs); + } + public IActionResult Create() + { + + return View(); + } + + [HttpPost] + [ValidateAntiForgeryToken] + public IActionResult Create(Blogs input) + { var maxId=Db.Blogs.Select(t=>t.Id).Max(); + input.Id=maxId+1; + Db.Blogs.Add(input); + + return RedirectToAction("Index"); + } + public IActionResult Edit() + { + + return View(); + } + public IActionResult Delete() + { + return View(); + } +} + +//模型 +Blogs类 +namespace Blog.Models; +public class Blogs{ + public int Id{get;set;} + public string Title{get;set;}=null!; + public string Content{get;set;}=null!; + public string Author{get;set;}=null!; +} + +//Db静态类 +namespace Blog.Models; +public static class Db{ + public static ListId | +标题 | +内容 | +作者 | +操作 | +
---|---|---|---|---|
@blog.Id | +@blog.Title | +@blog.Content | +@blog.Author | ++ 编辑 + 删除 + | +
产品ID | +产品名称 | +价格 | +
---|---|---|
@product.ProductId | +@product.Name | +@product.Price | +
Learn about building Web apps with ASP.NET Core.
-Use this page to detail your site's privacy policy.
diff --git "a/\351\273\216\346\254\243\346\254\243/\344\275\234\344\270\232/MVC\347\273\203\344\271\240/Blog/Views/Products/Create.cshtml" "b/\351\273\216\346\254\243\346\254\243/\344\275\234\344\270\232/MVC\347\273\203\344\271\240/Blog/Views/Products/Create.cshtml" deleted file mode 100644 index 05436b09dc8a0a6a45623d109cdd2767a61c8dfe..0000000000000000000000000000000000000000 --- "a/\351\273\216\346\254\243\346\254\243/\344\275\234\344\270\232/MVC\347\273\203\344\271\240/Blog/Views/Products/Create.cshtml" +++ /dev/null @@ -1,7 +0,0 @@ -@model YourNamespace.Models.Student - -The student's name is: @Model.Name
-The student's age is: @Model.Age
-The student's height is: @Model.Height
\ No newline at end of file diff --git "a/\351\273\216\346\254\243\346\254\243/\344\275\234\344\270\232/MVC\347\273\203\344\271\240/Blog/Views/Shared/Error.cshtml" "b/\351\273\216\346\254\243\346\254\243/\344\275\234\344\270\232/MVC\347\273\203\344\271\240/Blog/Views/Shared/Error.cshtml" deleted file mode 100644 index a1e04783c67a0edd13abb2745ca97f3a0bc6bc93..0000000000000000000000000000000000000000 --- "a/\351\273\216\346\254\243\346\254\243/\344\275\234\344\270\232/MVC\347\273\203\344\271\240/Blog/Views/Shared/Error.cshtml" +++ /dev/null @@ -1,25 +0,0 @@ -@model ErrorViewModel -@{ - ViewData["Title"] = "Error"; -} - -
- Request ID: @Model.RequestId
-
- Swapping to Development environment will display more detailed information about the error that occurred. -
-- The Development environment shouldn't be enabled for deployed applications. - It can result in displaying sensitive information from exceptions to end users. - For local debugging, enable the Development environment by setting the ASPNETCORE_ENVIRONMENT environment variable to Development - and restarting the app. -
diff --git "a/\351\273\216\346\254\243\346\254\243/\344\275\234\344\270\232/MVC\347\273\203\344\271\240/Blog/Views/Shared/_Layout.cshtml" "b/\351\273\216\346\254\243\346\254\243/\344\275\234\344\270\232/MVC\347\273\203\344\271\240/Blog/Views/Shared/_Layout.cshtml" deleted file mode 100644 index 2442e8ee59f62f5414dce3928928aaca4ef60d2a..0000000000000000000000000000000000000000 --- "a/\351\273\216\346\254\243\346\254\243/\344\275\234\344\270\232/MVC\347\273\203\344\271\240/Blog/Views/Shared/_Layout.cshtml" +++ /dev/null @@ -1,49 +0,0 @@ - - - - - -`s get reset. However, we also reset the\n// bottom margin to use `rem` units instead of `em`.\n\np {\n margin-top: 0;\n margin-bottom: $paragraph-margin-bottom;\n}\n\n\n// Abbreviations\n//\n// 1. Duplicate behavior to the data-bs-* attribute for our tooltip plugin\n// 2. Add the correct text decoration in Chrome, Edge, Opera, and Safari.\n// 3. Add explicit cursor to indicate changed behavior.\n// 4. Prevent the text-decoration to be skipped.\n\nabbr[title],\nabbr[data-bs-original-title] { // 1\n text-decoration: underline dotted; // 2\n cursor: help; // 3\n text-decoration-skip-ink: none; // 4\n}\n\n\n// Address\n\naddress {\n margin-bottom: 1rem;\n font-style: normal;\n line-height: inherit;\n}\n\n\n// Lists\n\nol,\nul {\n padding-left: 2rem;\n}\n\nol,\nul,\ndl {\n margin-top: 0;\n margin-bottom: 1rem;\n}\n\nol ol,\nul ul,\nol ul,\nul ol {\n margin-bottom: 0;\n}\n\ndt {\n font-weight: $dt-font-weight;\n}\n\n// 1. Undo browser default\n\ndd {\n margin-bottom: .5rem;\n margin-left: 0; // 1\n}\n\n\n// Blockquote\n\nblockquote {\n margin: 0 0 1rem;\n}\n\n\n// Strong\n//\n// Add the correct font weight in Chrome, Edge, and Safari\n\nb,\nstrong {\n font-weight: $font-weight-bolder;\n}\n\n\n// Small\n//\n// Add the correct font size in all browsers\n\nsmall {\n @include font-size($small-font-size);\n}\n\n\n// Mark\n\nmark {\n padding: $mark-padding;\n background-color: $mark-bg;\n}\n\n\n// Sub and Sup\n//\n// Prevent `sub` and `sup` elements from affecting the line height in\n// all browsers.\n\nsub,\nsup {\n position: relative;\n @include font-size($sub-sup-font-size);\n line-height: 0;\n vertical-align: baseline;\n}\n\nsub { bottom: -.25em; }\nsup { top: -.5em; }\n\n\n// Links\n\na {\n color: $link-color;\n text-decoration: $link-decoration;\n\n &:hover {\n color: $link-hover-color;\n text-decoration: $link-hover-decoration;\n }\n}\n\n// And undo these styles for placeholder links/named anchors (without href).\n// It would be more straightforward to just use a[href] in previous block, but that\n// causes specificity issues in many other styles that are too complex to fix.\n// See https://github.com/twbs/bootstrap/issues/19402\n\na:not([href]):not([class]) {\n &,\n &:hover {\n color: inherit;\n text-decoration: none;\n }\n}\n\n\n// Code\n\npre,\ncode,\nkbd,\nsamp {\n font-family: $font-family-code;\n @include font-size(1em); // Correct the odd `em` font sizing in all browsers.\n direction: ltr #{\"/* rtl:ignore */\"};\n unicode-bidi: bidi-override;\n}\n\n// 1. Remove browser default top margin\n// 2. Reset browser default of `1em` to use `rem`s\n// 3. Don't allow content to break outside\n\npre {\n display: block;\n margin-top: 0; // 1\n margin-bottom: 1rem; // 2\n overflow: auto; // 3\n @include font-size($code-font-size);\n color: $pre-color;\n\n // Account for some code outputs that place code tags in pre tags\n code {\n @include font-size(inherit);\n color: inherit;\n word-break: normal;\n }\n}\n\ncode {\n @include font-size($code-font-size);\n color: $code-color;\n word-wrap: break-word;\n\n // Streamline the style when inside anchors to avoid broken underline and more\n a > & {\n color: inherit;\n }\n}\n\nkbd {\n padding: $kbd-padding-y $kbd-padding-x;\n @include font-size($kbd-font-size);\n color: $kbd-color;\n background-color: $kbd-bg;\n @include border-radius($border-radius-sm);\n\n kbd {\n padding: 0;\n @include font-size(1em);\n font-weight: $nested-kbd-font-weight;\n }\n}\n\n\n// Figures\n//\n// Apply a consistent margin strategy (matches our type styles).\n\nfigure {\n margin: 0 0 1rem;\n}\n\n\n// Images and content\n\nimg,\nsvg {\n vertical-align: middle;\n}\n\n\n// Tables\n//\n// Prevent double borders\n\ntable {\n caption-side: bottom;\n border-collapse: collapse;\n}\n\ncaption {\n padding-top: $table-cell-padding-y;\n padding-bottom: $table-cell-padding-y;\n color: $table-caption-color;\n text-align: left;\n}\n\n// 1. Removes font-weight bold by inheriting\n// 2. Matches default `