diff --git "a/\345\274\240\344\277\212\346\235\260/20241114-\350\241\250\346\240\274.md" "b/\345\274\240\344\277\212\346\235\260/20241114-\350\241\250\346\240\274.md" new file mode 100644 index 0000000000000000000000000000000000000000..92b7ecdd82824ac577bef6bf1921788f38117b40 --- /dev/null +++ "b/\345\274\240\344\277\212\346\235\260/20241114-\350\241\250\346\240\274.md" @@ -0,0 +1,12 @@ +dotnet new sln -n解决方案名 + +创建mvc项目 +dotnet new mvc -o mvc项目名 + +创建类库 +dotnet new classlib -o 类库名 + +程序启动 +dotnet run + +![](./微信图片_20241117225334.png) \ No newline at end of file diff --git "a/\345\274\240\344\277\212\346\235\260/20241114\351\241\271\347\233\256.md" "b/\345\274\240\344\277\212\346\235\260/20241114\351\241\271\347\233\256.md" new file mode 100644 index 0000000000000000000000000000000000000000..ff348c541d9cc8b1ac0b9046621bfc63c31d076c --- /dev/null +++ "b/\345\274\240\344\277\212\346\235\260/20241114\351\241\271\347\233\256.md" @@ -0,0 +1,14 @@ +## 项目结构 +创建的 MVC 项目会包含以下目录和文件: + +Controllers:控制器类,负责处理用户的请求并返回响应。 + +Views:视图文件,使用 Razor 语法来呈现 HTML 页面。 + +Models:用于存放数据模型的类。 + +wwwroot:包含静态文件,如 CSS、JavaScript 和图片。 + +appsettings.json:应用程序的配置文件。 + +Program.cs:配置和启动应用程序的入口点。 \ No newline at end of file diff --git "a/\345\274\240\344\277\212\346\235\260/20241119\347\254\224\350\256\260.md" "b/\345\274\240\344\277\212\346\235\260/20241119\347\254\224\350\256\260.md" new file mode 100644 index 0000000000000000000000000000000000000000..960da324a97971ab7df3ae61cab706662d2ce901 --- /dev/null +++ "b/\345\274\240\344\277\212\346\235\260/20241119\347\254\224\350\256\260.md" @@ -0,0 +1,21 @@ +## 登服务器操作 +1、ssh root@域名 +2、安装nginx apt install -y ninx +3、测试 systemctl status nginx +4、安装sdk +更新软件源 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/\345\274\240\344\277\212\346\235\260/20241121\347\254\224\350\256\260.md" "b/\345\274\240\344\277\212\346\235\260/20241121\347\254\224\350\256\260.md" new file mode 100644 index 0000000000000000000000000000000000000000..8e51821c0f668532dcc692d1be12e6f74a541ee6 --- /dev/null +++ "b/\345\274\240\344\277\212\346\235\260/20241121\347\254\224\350\256\260.md" @@ -0,0 +1,34 @@ +# 传参 + +## 通过表单传递参数: +当使用表单提交数据时,可以通过模型绑定将表单字段绑定到Action方法的参数。 +```html +public class Item +{ + public int Id { get; set; } + public string Name { get; set; } +} +``` + +#3 通过路由传递参数: +在控制器的Action中,你可以通过在路由模板中定义参数来接收参数。 + +```html + GET: Home/Details/5 +public IActionResult Details(int id) +{ + var item = _context.Items.FirstOrDefault(m => m.Id == id); + return View(item); +} +``` + +## 通过Header传递参数: +```html + +可以通过[FromHeader]属性来获取HTTP请求头中的参数。 + +public IActionResult GetSecretData([FromHeader] string secretKey) +{ + // 根据secretKey参数执行操作 +} +``` \ No newline at end of file diff --git "a/\345\274\240\344\277\212\346\235\260/20241122\347\254\224\350\256\260.md" "b/\345\274\240\344\277\212\346\235\260/20241122\347\254\224\350\256\260.md" new file mode 100644 index 0000000000000000000000000000000000000000..23daf95953a7d83f80cc5f7b4e49cff33b3d8d71 --- /dev/null +++ "b/\345\274\240\344\277\212\346\235\260/20241122\347\254\224\350\256\260.md" @@ -0,0 +1,26 @@ +# 返回 +## 制器返回类型 +一般数据类型直接返回 如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等。 +```html +public IActionResult Index() +{ + return View(); +} +ViewResult: +返回一个视图,通常用于渲染HTML页面。 +``` +```html +public IActionResult Index() +{ + return View(); +} +``` \ No newline at end of file diff --git "a/\345\274\240\344\277\212\346\235\260/20241124165032.png" "b/\345\274\240\344\277\212\346\235\260/20241124165032.png" new file mode 100644 index 0000000000000000000000000000000000000000..2c1b2b8ab331bef8cd171b0aeb1a32e20d2bab28 Binary files /dev/null and "b/\345\274\240\344\277\212\346\235\260/20241124165032.png" differ diff --git "a/\345\274\240\344\277\212\346\235\260/Linux\347\273\203\344\271\240.md" "b/\345\274\240\344\277\212\346\235\260/Linux\347\273\203\344\271\240.md" new file mode 100644 index 0000000000000000000000000000000000000000..65bcacac4128c56c5db4a64aa5b4ab98b30999d3 --- /dev/null +++ "b/\345\274\240\344\277\212\346\235\260/Linux\347\273\203\344\271\240.md" @@ -0,0 +1,6 @@ +## 效果: + + +![](./20241124165032.png) + + diff --git "a/\345\274\240\344\277\212\346\235\260/MVC\347\273\203\344\271\240.md" "b/\345\274\240\344\277\212\346\235\260/MVC\347\273\203\344\271\240.md" new file mode 100644 index 0000000000000000000000000000000000000000..01871b1a6c098f43ca24de2aae265d2022ce82ff --- /dev/null +++ "b/\345\274\240\344\277\212\346\235\260/MVC\347\273\203\344\271\240.md" @@ -0,0 +1,73 @@ +1.创建一个控制台项目 +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 + + +7. + public IActionResult Ok() + { + return View(); + } + +8. + public class BlogsController : Controller + { + public IActionResult Index() + { + return View(); + } + } + + 9. + public IActionResult Music() + { + return View(); + } +10 + public IActionResult List() + { + return View(); + } +11. +public class ProductsController : Controller +{ + public IActionResult Edit(int id) + { + ViewBag.Id = id; + return View(); + } +} + +12. +public class ProductsController : Controller{ + public IActionResult Create(){ + var pro=new ProductStudent{ + Name="小怡", + Age="19", + Tall="185" + }; + return View(pro); +} + } + public class ProductStudent{ + public string Name{get;set;}=null!; + public string Age{get;set;}=null!; + public string Tall{get;set;}=null!; + + } \ No newline at end of file diff --git "a/\345\274\240\344\277\212\346\235\260/readme.md" "b/\345\274\240\344\277\212\346\235\260/readme.md" new file mode 100644 index 0000000000000000000000000000000000000000..afe258797b50f7d30e1aa239c313d07833d67b3e --- /dev/null +++ "b/\345\274\240\344\277\212\346\235\260/readme.md" @@ -0,0 +1,4 @@ +## 23级软件5班 MVC课堂笔记 +上课录屏地址:https://www.alipan.com/s/HMxZbQeNagN + +石可破也,而不可夺坚;丹可磨也,而不可夺赤。《吕氏春秋.诚廉》 \ No newline at end of file diff --git "a/\345\274\240\344\277\212\346\235\260/\344\270\223\351\241\271\347\273\203\344\271\240-\345\237\272\347\241\200\350\203\275\345\212\233.md" "b/\345\274\240\344\277\212\346\235\260/\344\270\223\351\241\271\347\273\203\344\271\240-\345\237\272\347\241\200\350\203\275\345\212\233.md" new file mode 100644 index 0000000000000000000000000000000000000000..5140987a8d0460d321af5c938e7b7f7f3d894413 --- /dev/null +++ "b/\345\274\240\344\277\212\346\235\260/\344\270\223\351\241\271\347\273\203\344\271\240-\345\237\272\347\241\200\350\203\275\345\212\233.md" @@ -0,0 +1,53 @@ +# 生成一个随机整数,范围[0,100],注意是否包含 +```html +Random random = new Random(); +int randomNumberInclusive = random.Next(0, 101); +Console.WriteLine($"随机数(包含0和100):{randomNumberInclusive}"); +``` +# 生成一个随机整数,范围(0,100],注意是否包含 +```html +Random random = new Random(); +int randomNumberExclusive = random.Next(1, 101); +Console.WriteLine($"随机数(不包含0,包含100):{randomNumberExclusive}"); +``` +# 生成10个随机整数,范围[5,80],注意是否包含 +```html +Random random = new Random(); +List randomNumbers = new List(); +for (int i = 0; i < 10; i++) +{ + int randomNumber = random.Next(5, 81); + randomNumbers.Add(randomNumber); +} +Console.WriteLine("随机数列表(每个在[5, 80]范围内):"); +foreach (var num in randomNumbers) +{ + Console.WriteLine(num); +} +``` +# 定义一个字符串,字符串中有100个中文字符,需要从中随机取1个字符串 +```html +string chineseCharacters = "汉 字 列 表 示 例 子 随 机 生 成 的 一 百 个 汉 字 这 是 一 个 大 型 的 汉 字 集 合 体 系 列 所 有 的 汉 字 都 是 从 中 华 人 民 共 和 国 标 准 字 体 中 抽 取 的 每 一个 汉 字 都 是 独 特 的 不 可 替 换 的 这 些 汉 字 包 括 了 从 简 体 到 繁 体 的 各 种 形 式"; +Random random = new Random(); +char randomChar = chineseCharacters[random.Next(chineseCharacters.Length)]; +Console.WriteLine($"随机选取的字符:{randomChar}"); +``` + +# 定义一个字符串,字符串中有100个中文字符,需要从中随机取5-50个字符,组成新的字符 +```html +string chineseCharacters = "汉 字 列 表 示 例 子 随 机 生 成 的 一 百 个 汉 字 这 是 一 个 大 型 的 汉 字 集 合 体 系 列 所 有 的 汉 字 都 是 从 中 华 人 民 共 和 国 标 准 字 体 中 抽 取 的 每 一个 汉 字 都 是 独 特 的 不 可 替 换 的 这 些 汉 字 包 括 了 从 简 体 到 繁 体 的 各 种 形 式"; +Random random = new Random(); +int length = random.Next(5, 51); // 随机长度在5到50之间 +string randomSubstring = new string(chineseCharacters.OrderBy(x => random.Next()).Take(length).ToArray()); +Console.WriteLine($"随机选取的子字符串:{randomSubstring}"); +``` +# 定义2个字符串,第一个字符串中放百家姓,第二个字符串中放中文字符,要求从第一个字符串随机取得一个姓,再从第二个字符串中随机获得1到2个字符组成新字符串,和第一个字符串取得的姓组成一个姓名 +``` +string chineseCharacters = "汉 字 列 表 示 例 子 随 机 生 成 的 一 百 个 汉 字 这 是 一 个 大 型 的 汉 字 集 合 体 系 列 所 有 的 汉 字 都 是 从 中 华 人 民 共 和 国 标 准 字 体 中 抽 取 的 每 一个 汉 字 都 是 独 特 的 不 可 替 换 的 这 些 汉 字 包 括 了 从 简 体 到 繁 体 的 各 种 形 式"; +Random random = new Random(); +string surname = surnameString[random.Next(surnameString.Length)]; +int nameLength = random.Next(1, 3); // 随机长度在1到2之间 +string randomNamePart = new string(chineseCharacters.OrderBy(x => random.Next()).Take(nameLength).ToArray()); +string fullName = surname + randomNamePart; +Console.WriteLine($"随机生成的姓名:{fullName}"); +``` \ No newline at end of file diff --git "a/\345\274\240\344\277\212\346\235\260/\344\270\223\351\241\271\347\273\203\344\271\240-\346\216\247\345\210\266\345\231\250\344\274\240\345\217\202.md" "b/\345\274\240\344\277\212\346\235\260/\344\270\223\351\241\271\347\273\203\344\271\240-\346\216\247\345\210\266\345\231\250\344\274\240\345\217\202.md" new file mode 100644 index 0000000000000000000000000000000000000000..c95ff799463d476466a1841395f44c16c24e432c --- /dev/null +++ "b/\345\274\240\344\277\212\346\235\260/\344\270\223\351\241\271\347\273\203\344\271\240-\346\216\247\345\210\266\345\231\250\344\274\240\345\217\202.md" @@ -0,0 +1,92 @@ +1、简单参数传递 在一个叫Blog控制器中,定义一个叫Index的Action,并且传递一个int类型的值,id为变量名 +```html + public class BlogController : Controller + { + public IActionResult Index(int id) + { + return Content(id.ToString) + } + + } + ``` +2、简单参数传递 在一个叫Blog控制器中,定义一个叫Index_2的Action,并且传递一个string类型的值,id为变量名 +```html +public class BlogController : Controller + { + + public IActionResult Index_2(string id) + { + return Content(id) + } + + } + ``` +3、简单参数传递 在一个叫Blog控制器中,定义一个叫Index_3的Action,并且传递一个string类型的值,name为变量名 +```html +public class BlogController : Controller + { + public IActionResult Index_3(string name) + { + return Content(name) + } + } + ``` +4、复杂参数传递 在一个叫Blog的控制器中,定义一个名为Create的Action,并且传递一个BlogCreateDto类型的值,blogCreateDto为变量名 +```html +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为变量名 +```html +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为变量名 +```html +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/\345\274\240\344\277\212\346\235\260/\344\270\223\351\241\271\347\273\203\344\271\240-\346\216\247\345\210\266\345\231\250\350\277\224\345\233\236\345\200\274.md" "b/\345\274\240\344\277\212\346\235\260/\344\270\223\351\241\271\347\273\203\344\271\240-\346\216\247\345\210\266\345\231\250\350\277\224\345\233\236\345\200\274.md" new file mode 100644 index 0000000000000000000000000000000000000000..eb790a5e0ed244434c91e284d9cb7eaa511de0ba --- /dev/null +++ "b/\345\274\240\344\277\212\346\235\260/\344\270\223\351\241\271\347\273\203\344\271\240-\346\216\247\345\210\266\345\231\250\350\277\224\345\233\236\345\200\274.md" @@ -0,0 +1,9 @@ +## 控制器返回类型 +一般数据类型直接返回 如int、double、string、IEnumerable等数据类型 +IActionResult 一个接口,用于返回HTTP信息状态,如200、401、404等 +视图 +重定向 +ActionResult类型 将一般数据类型和HTTP状态信息混合使用 +特定于格式的操作结果:如JsonResult和ContentResult +POCO(普通旧CLR对象) + diff --git "a/\345\274\240\344\277\212\346\235\260/\345\276\256\344\277\241\345\233\276\347\211\207_20241117225334.png" "b/\345\274\240\344\277\212\346\235\260/\345\276\256\344\277\241\345\233\276\347\211\207_20241117225334.png" new file mode 100644 index 0000000000000000000000000000000000000000..a8c60c582615a81b97ee56b33111c867e4adf4f1 Binary files /dev/null and "b/\345\274\240\344\277\212\346\235\260/\345\276\256\344\277\241\345\233\276\347\211\207_20241117225334.png" differ