12.1 ASP.NET Core 简介
ASP.NET Core 是一个跨平台的高性能框架,用于构建现代 Web 应用程序和 API。
12.2 MVC 模式
MVC(Model-View-Controller)是一种设计模式,用于将应用程序的逻辑、数据和用户界面分离。
示例代码:
csharp
复制
// Controller
public class HomeController : Controller
{
public IActionResult Index()
{
return View();
}
}
// View (Index.cshtml)
@{
ViewData["Title"] = "Home Page";
}
Welcome
Learn about building Web apps with ASP.NET Core.
12.3 Razor 页面
Razor 页面是 ASP.NET Core 中的一种简化模型,用于构建页面驱动的 Web 应用程序。
示例代码:
csharp
复制
// PageModel
public class IndexModel : PageModel
{
public void OnGet()
{
}
}
// Razor Page (Index.cshtml)
@page
@model IndexModel
@{
ViewData["Title"] = "Home page";
}
Welcome
Learn about building Web apps with ASP.NET Core.
12.4 Web API 开发
ASP.NET Core 提供了强大的支持来构建 RESTful Web API。
示例代码:
csharp
复制
// Controller
[ApiController]
[Route("api/[controller]")]
public class ValuesController : ControllerBase
{
[HttpGet]
public IEnumerable Get()
{
return new string[] { "value1", "value2" };
}
[HttpGet("{id}")]
public string Get(int id)
{
return "value";
}
[HttpPost]
public void Post([FromBody] string value)
{
}
[HttpPut("{id}")]
public void Put(int id, [FromBody] string value)
{
}
[HttpDelete("{id}")]
public void Delete(int id)
{
}
}
课后练习题
- 简答题:
- 什么是 ASP.NET Core?它的主要优势是什么?
- MVC 模式的主要组成部分是什么?它们的作用是什么?
- 什么是 Razor 页面?它与 MVC 模式有什么区别?
- 编程题:
- 创建一个 ASP.NET Core MVC 应用程序,实现一个简单的用户注册页面。
- 编写一个 Razor 页面应用程序,显示当前日期和时间。
- 创建一个 ASP.NET Core Web API,实现基本的 CRUD 操作。