上善若水

水善利万物而不争
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

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

Posted on 2022-07-14 21:43  董锡振  阅读(390)  评论(0编辑  收藏  举报

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

笔记如下:

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

 

 

 

 

F5调试运行,如图:

 

 

 

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

运行页面如下:

代码贴出入下:

Models 下创建 StudViewModel

   public class StudViewModel
    {
        public string Name { get; set; }
        public bool Sex { get; set; }
        public int Age { get; set; }
    }

 Views 下修改页面:

@{
    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>
*@


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 });
        //}
    }