上善若水

水善利万物而不争
随笔 - 175, 文章 - 0, 评论 - 10, 阅读 - 14万
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

.NETCore | MVC | 1 使用项目模板 创建.NET6 MVC 项目

Posted on   董锡振  阅读(420)  评论(0编辑  收藏  举报

写在前,谢谢大师的视频:https://www.bilibili.com/video/BV1mY411K7C5?p=1

笔记如下:

使用VS2022新建项目,选择 .NET Core Web应用(模型-视图-控制器),取消【配置 HTTPS(H)】选项,如图:

 

 

 

 

F5调试运行,如图:

 

 

 

添加自己的代码,目的是体验一次从Model ->View->Control 的过程:

运行页面如下:

代码贴出入下:

Models 下创建 StudViewModel

1
2
3
4
5
6
public class StudViewModel
 {
     public string Name { get; set; }
     public bool Sex { get; set; }
     public int Age { get; set; }
 }

 Views 下修改页面:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
@{
    ViewData["Title"] = "Home Page";
}
@model MVC01.Models.StudViewModel
<html>
<head>
    <tile>学生信息</tile>
</head>
<body>
    <table>
        <tr>
            <td>姓名</td>
            <td>性别</td>
            <td>年龄</td>
        </tr>
        <tr>
            <td>@Model.Name</td>
            <td>@Model.Sex</td>
            <td>@Model.Age</td>
        </tr>
    </table>
 
</body>
</html>
@*<div class="text-center">
    <h1 class="display-4">Welcome</h1>
    <p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
</div>
*@<br><br><br>COntrollers下修改为自己的代码:    public class HomeController : Controller
    {
        public IActionResult Index()
        {
            StudViewModel stu = new StudViewModel()
            {
                Name = "dong",
                Sex = true,
                Age = 18
            };
            return View(stu);
        }
        //注释掉原有内容 如上的Index方法中添加自己的代码 把Stu返回给前端view
        //public IActionResult Index()
        //{
        //    return View();
        //}
        //private readonly ILogger<HomeController> _logger;
        //public HomeController(ILogger<HomeController> logger)
        //{
        //    _logger = logger;
        //} 
        //public IActionResult Privacy()
        //{
        //    return View();
        //}
        //[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        //public IActionResult Error()
        //{
        //    return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        //}
    }

  

相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
点击右上角即可分享
微信分享提示