diff --git "a/\345\221\250\351\231\210\345\256\271/20241119-Linux.md" "b/\345\221\250\351\231\210\345\256\271/20241119-Linux.md" new file mode 100644 index 0000000000000000000000000000000000000000..5c408ea03b00a87395801fc02163528ff07771a9 --- /dev/null +++ "b/\345\221\250\351\231\210\345\256\271/20241119-Linux.md" @@ -0,0 +1,13 @@ +### 服务器没备案。。。 +``` +tabby应用:配置和连接 新建SSH连接 +``` +``` +xShell:点击“新建”,新建会话 + *填写一个便于标识的名称 + *填写服务器的公网IP + *点击用户身份验证 填写服务器的用户名和密码,之后点击“确定” 填写服务器的用户名和密码,之后点击“确定” +``` +``` +putty:配置连接:用户需要输入需要连接的主机或IP地址、端口号等信息,并点击“save”按钮 选中之前保持好的配置,点击“open”打开 +``` diff --git "a/\345\221\250\351\231\210\345\256\271/20241121-\347\273\203\344\271\240.md" "b/\345\221\250\351\231\210\345\256\271/20241121-\347\273\203\344\271\240.md" new file mode 100644 index 0000000000000000000000000000000000000000..443417d82ad8fb4124ce19130780d6ac6b145995 --- /dev/null +++ "b/\345\221\250\351\231\210\345\256\271/20241121-\347\273\203\344\271\240.md" @@ -0,0 +1,82 @@ +### mvc练习 +``` +dotnet new mvc -o .\名称\ +dotnet run --project .\名称\ +dotnet new sln -o .\名称\ +dotnet publish 将程序打包到服务器 +dotnet new mvc -o .\名称\ +``` + +``` +1、创建一个控制台项目,没有任何选项,体会项目名称和什么有关系 +dotnet new console +``` +``` +2.创建一个控制项目,项目名称Blog + public IActionResult Hello() + { + return View(); + } +``` +``` +3、创建一个控制台项目,输出到Blog目录 +dotnet new console -o Bolgs +``` +``` +4、创建一个MVC项目,指定项目名称 +dotnet new sln -o FaceBook +``` +``` +5、创建一个MVC项目,指定输出目录 +dotnet new sln -n FaceBook +``` +``` +6、创建一个带解决方案,其下有一个MVC项目,3个类库项目的“综合项目” + dotnet new sln -o FaceBook + dotnet new mvc -o src/Face.Web + dotnet new classlib -o src/Face.Core(类库项目) + dotnet new classlib -o src/Face.Domain(类库项目) + dotnet new classlib -o src/Face.Application(类库项目) + 【创建的解决方案与项目啥的都没关系】 + 想要解决方案管理它旗下的项目——dotnet sln add .\src\Face.Domain\ + dotnet sln add .\src\Blog.Face.Domain\ + dotnet sln add .\src\Blog.Face.Application\ +``` +``` +7、创建一个项目,在默认控制器(Home)下,新增一个Action方法,名为Ok,同时为其创建对应视图以显示这个视图 +先在Views Home里手动创建一个Ok.cshtml文件 +再在HomeController中 +public IActionResult Ok(){ + return View(); + } +``` +``` +8、创建一个项目,创建一个新的控制器,名为Blogs,新的控制器拥有一个名为Index的Action,该方法返回一个视图,视图显示“神级预判” +8图片 +``` +```9、给第8题的新控制,添加一个新的Action,名为Music,不接受任何参数,并返回对应的视图,视图显示“顶级打野” +9图片 +``` +``` +10、给第8题的新控制器,新增一个Action,名为List,不接受任何参数,并返回对应视图,视图显示一个经典CRUD界面 +10图片 +``` +``` +11、新增一个控制器,名为Products,该控制器具有一个名为Edit的Action,这个Action接受一个int类型的参数id,显示这个id + 先在Views 创建一个Products 再在这里手动创建一个Edit.cshtml文件 + 再在HomeController中 + public IActionResult Edit(int id) + { + ViewBag.Id = id; + return View(); + } +``` +``` +12、在11题的新控制器中,新增一个名为Create的Action,该Action接受一个类型为Students(有姓名、年龄、体长属性)的参数,并展示该参数的姓名属性 + public class Students + { + public required string Name { get; set; } + public int Age { get; set; } + public double Height { get; set; } + } +``` diff --git "a/\345\221\250\351\231\210\345\256\271/20241122-\346\216\247\345\210\266\345\231\250.md" "b/\345\221\250\351\231\210\345\256\271/20241122-\346\216\247\345\210\266\345\231\250.md" new file mode 100644 index 0000000000000000000000000000000000000000..a2350f67976101797d5a19452cd78c0178a6b74e --- /dev/null +++ "b/\345\221\250\351\231\210\345\256\271/20241122-\346\216\247\345\210\266\345\231\250.md" @@ -0,0 +1,206 @@ +### 笔记 +``` +控制器返回类型 +- 一般数据类型直接返回 如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等 +``` +``` +csharp +public IActionResult Index() +{ + return View(); +} +``` +``` +ViewResult:返回一个视图,通常用于渲染HTML页面 +```csharp +public IActionResult Index() +{ + return View(); +} +``` +``` +JsonResult:返回一个包含JSON数据的HTTP响应 +``` +csharp +public IActionResult GetJson() +{ + var data = new { Name = "张三" }; + return Json(data); +} +``` + +## ContentResult: +返回一个包含纯文本内容的HTTP响应 +```csharp +public IActionResult GetText() +{ + return Content("Hello, World!", "text/plain"); +} +``` + + +### 1、渲染简单数据到页面 +``` +c# + public class EndController : Controller + { + [HttpGet] + public IActionResult Editone(int id) +{ + var list=new List{ + new(){ + Title="撒野", + Author="巫哲", + Content="缉毒", + Name="破云" + }, + new(){ + Title="撒野", + Author="巫哲", + Content="缉毒", + Name="破云" + }, + new(){ + Title="撒野", + Author="巫哲", + Content="缉毒", + Name="破云" + }, + new(){ + Title="撒野", + Author="巫哲", + Content="缉毒", + Name="破云" + }, + new(){ + Title="撒野", + Author="巫哲", + Content="缉毒", + Name="破云" + }, + new(){ + Title="撒野", + Author="巫哲", + Content="缉毒", + Name="破云" + } + }; + 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!; + +} + +``` + +``` +html +@model List + + + + + + + + + + @foreach(var item in @Model){ + + + + + + + + } +
标题作者内容姓名操作
@item.Title@item.Author@item.Content@item.Name + + +
+``` + +``` +## 2、渲染复杂数据到页面 +public class BlogPost +{ + public int Id { get; set; } + public string Title { get; set; } + public string Content { get; set; } + public DateTime PublishDate { get; set; } + // 可以添加更多属性来表示复杂数据 +} +public class BlogController : Controller +{ + // GET: Blog/PostDetail/1 + public ActionResult PostDetail(int id) + { + // 假设你有一个方法来获取博客文章 + BlogPost post = GetBlogPostById(id); + + // 将模型传递给视图 + return View(post); + } + + // 这是一个模拟方法,用于获取博客文章数据 + private BlogPost GetBlogPostById(int id) + { + // 这里应该是从数据库或其他数据源获取数据的逻辑 + // 但为了简化,我们返回一个硬编码的对象 + return new BlogPost + { + Id = id, + Title = "示例博客文章", + Content = "这是博客文章的内容。", + PublishDate = DateTime.Now.AddDays(-7) // 假设文章发布于一周前 + }; + } +} +@model YourNamespace.Models.BlogPost + +@{ + ViewBag.Title = "博客文章详情"; +} + +

@Model.Title

+

发布日期: @Model.PublishDate.ToShortDateString()

+

@Model.Content

+``` +``` +## 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 diff --git "a/\345\221\250\351\231\210\345\256\271/20241122-\346\216\247\345\210\266\345\231\250\344\274\240\345\217\202.md" "b/\345\221\250\351\231\210\345\256\271/20241122-\346\216\247\345\210\266\345\231\250\344\274\240\345\217\202.md" new file mode 100644 index 0000000000000000000000000000000000000000..017e6b28f392efa6073506c615049504cb0cf0a8 --- /dev/null +++ "b/\345\221\250\351\231\210\345\256\271/20241122-\346\216\247\345\210\266\345\231\250\344\274\240\345\217\202.md" @@ -0,0 +1,104 @@ +### 控制器传参 +``` +地址传参:url/值 接收一般为id,可以在路由器更改 +``` +``` +using Microsoft.AspNetCore.Mvc; +namespace Blog.Controllers; +public class BlogController : Controller{ +1.简单参数传递 在一个叫Blog控制器中,定义一个叫Index的Action, +并且传递一个int类型的值,id为变量名 +public dynamic Index(int id){ + return new {Id=id}; +} +2.简单参数传递 在一个叫Blog控制器中, +定义一个叫Index_2的Action,并且传递一个string类型的值,id为变量名 +public IActionResult Index_2(int id){ + var azString="karry"; + var zzString="apt"; + var res=""; + if(id == 0){ + res=azString; + } + else if(id == 1){ +res=zzString; + } + else{ + res="额"; + } + return Content(string.Format("{0},hello",res)); +} +3.简单参数传递 在一个叫Blog控制器中, +定义一个叫Index_3的Action,并且传递一个string类型的值,name为变量名 +[HttpGet] +public Mypro Index_33(){ + var mypro=new Mypro{ + Name="karry" + }; + return mypro; +} +[HttpGet] +4.一个叫Blog的控制器中,定义一个名为Create的Action, +并且传递一个BlogCreateDto类型的值,blogCreateDto为变量名 +public BlogCreateDto Create(){ + var blog=new BlogCreateDto{ + Title="不知道", + Author="丨", + Content="句号" + }; + return blog; +} +5.复杂参数传递 在一个叫Blog的控制器中,定义一个名为Create_1的Action, +并且传递一个Products类型的值,productCreateDto为变量名 +public Products Create_1(){ + var productCreateDto=new Products{ + Names="无名", + Price="100", + Stock="啥" + }; + return productCreateDto; +} +6.复杂参数传递 在一个叫Blog的控制器中,定义一个名为Create_2的Action, +并且传递一个Students类型的值,studentCreateDto为变量名 +public Students Create_2(){ + var studentCreateDto=new Students{ + StudentName="星", + Sex="女", + Age="18" + }; + return studentCreateDto; +} +} + +public class Mypro{ + public string Name{get;set;}=null!; +} + +4. +public class BlogCreateDto{ + public string Title{get;set;}=null!; + public string Author{get;set;}=null!; + public string Content{get;set;}=null!; + +} +5. +public class Products{ + public string Names{get;set;}=null!; + public string Price{get;set;}=null!; + public string Stock{get;set;}=null!; +} + +6. +public class Students{ + public string StudentName{get;set;}=null!; + public string Sex{get;set;}=null!; + public string Age{get;set;}=null!; +} + +views() +@model Blog.Controllers.BlogCreateDto; +@Model.Title; +@Model.Author; +@Model.Content +``` +``` \ No newline at end of file diff --git "a/\345\221\250\351\231\210\345\256\271/20241122-\351\232\217\346\234\272\346\225\260.md" "b/\345\221\250\351\231\210\345\256\271/20241122-\351\232\217\346\234\272\346\225\260.md" new file mode 100644 index 0000000000000000000000000000000000000000..1bb7d8b50ff92519798d603ea0328267e7988a01 --- /dev/null +++ "b/\345\221\250\351\231\210\345\256\271/20241122-\351\232\217\346\234\272\346\225\260.md" @@ -0,0 +1,64 @@ +``` +using Microsoft.AspNetCore.Mvc; +namespace Blog.Controllers; + +public class RandController : Controller{ +// 1.生成一个随机整数,范围[0,100],注意是否包含 +public IActionResult Index(){ + RandomNumber model=new RandomNumber(); + Random random=new Random(); + model.Randsnumber=random.Next(0,101); + return View(model); + +} +// 2.生成一个随机整数,范围(0,100],注意是否包含 +public IActionResult Itwo(){ + RandTwo model=new RandTwo(); + Random rand=new Random(); + model.Randstwos=rand.Next(0,100); + return View(model); +} +//3.生成10个随机整数,范围[5,80],注意是否包含 +public IActionResult Ithree(){ + RandThree model=new RandThree(); + Random rands=new Random(); + model.Randsthrees=rands.Next(5,80); + return View(model); +} +// 4.定义一个字符串,字符串中有100个中文字符,需要从中随机取1个字符串 +public char GetRandomChineseCharacter() +{ + string chineseCharacters = "的一是在不了有和人这中大为上个国我以要他时来用们生到作地于出就分对成会可主发年动同工也能下过子说产种面而方后多定行学法所民得经十三之进样理心体信息东件感理"; + Random random = new Random(); + return chineseCharacters[random.Next(chineseCharacters.Length)]; +} + +// 5定义一个字符串,字符串中有100个中文字符,需要从中随机取5-50个字符,组成新的字符 +public string GetRandomChineseSubstring() +{ + string chineseCharacters = "的一是在不了有和人这中大为上个国我以要他时来用们生到作地于出就分对成会可主发年动同工也能下过子说产种面而方后多定行学法所民得经十三之进样理心体信息东件感理"; + Random random = new Random(); + int length = random.Next(5, 51); // 5到50个字符 + return new string(chineseCharacters.Take(length).ToArray()); +} +// 6. +} +// 1. +public class RandomNumber +{ + public int Randsnumber{get;set;} +} +// 2. +public class RandTwo{ + public int Randstwos{get;set;} +} +// 3. +public class RandThree{ + public int Randsthrees{get;set;} +} +// 4. +public class RandFour{ + public string Randfours{get;set;}=null!; +} + +``` \ No newline at end of file diff --git "a/\345\221\250\351\231\210\345\256\271/Imges/0.png" "b/\345\221\250\351\231\210\345\256\271/Imges/0.png" new file mode 100644 index 0000000000000000000000000000000000000000..ea436cbb1df3ffff764d1efb367708a9b7091330 Binary files /dev/null and "b/\345\221\250\351\231\210\345\256\271/Imges/0.png" differ diff --git "a/\345\221\250\351\231\210\345\256\271/Imges/10.png" "b/\345\221\250\351\231\210\345\256\271/Imges/10.png" new file mode 100644 index 0000000000000000000000000000000000000000..16c8e8bd029a66bc17062ac961e271e05812348c Binary files /dev/null and "b/\345\221\250\351\231\210\345\256\271/Imges/10.png" differ diff --git "a/\345\221\250\351\231\210\345\256\271/Imges/7.png" "b/\345\221\250\351\231\210\345\256\271/Imges/7.png" new file mode 100644 index 0000000000000000000000000000000000000000..caa895ba2bce99405bd4dd2e895b4ec4b5d0bc35 Binary files /dev/null and "b/\345\221\250\351\231\210\345\256\271/Imges/7.png" differ diff --git "a/\345\221\250\351\231\210\345\256\271/Imges/8.png" "b/\345\221\250\351\231\210\345\256\271/Imges/8.png" new file mode 100644 index 0000000000000000000000000000000000000000..b18f5ef6287197ad4ab8d44e188c4a09860d946f Binary files /dev/null and "b/\345\221\250\351\231\210\345\256\271/Imges/8.png" differ diff --git "a/\345\221\250\351\231\210\345\256\271/Imges/9.png" "b/\345\221\250\351\231\210\345\256\271/Imges/9.png" new file mode 100644 index 0000000000000000000000000000000000000000..093d4f90d2fb9beb6587cc4f7706e1c92515a7f8 Binary files /dev/null and "b/\345\221\250\351\231\210\345\256\271/Imges/9.png" differ