ASP .NET Razor Razor 是一种标记语法可以让您将基于服务器的代码Visual Basic 和 C#嵌入到网页中。基于服务器的代码可以在网页传送给浏览器时创建动态 Web 内容。当一个网页被请求时服务器在返回页面给浏览器之前先执行页面中的基于服务器的代码。通过服务器的运行代码能执行复杂的任务比如进入数据库。Razor 是基于 ASP.NET 的是为创建 Web 应用程序而设计的。它具有传统 ASP.NET 的功能但更容易使用并且更容易学习。简单说前后端在一起。在web api的基础上多了网页。创建项目安装模版如果没有dotnet new install Furion.Template.RazorWithWebApi查看C:\Users\aarons.chendotnet new list Furion這些範本符合您的輸入: Furion範本名稱 簡短名稱 語言 標記---------------------------------------- ------------------ ---- ---------------------------------Furion Api furionapi [C#] Furion/WebApi/Api/RESTful/SwaggerFurion Razor With WebApi furionrazorapi [C#] Furion/Razor/Pages/WebApi/Api创建dotnet new furionrazorapi -n 你的项目名称打开运行用vs打开点运行这是自带的。编写你的代码节省时间先把web api写好代码参考Web API C# (Furion版带 单元测试-CSDN博客项目结构和之前的web api结构一样只是在web.entry中多了Pages文件夹里面就是放你显示的网页。以例子为例User是我新建的直接看原本的Index.cshtml和Index.cshtml.csIndex.cshtml 和 Index.cshtml.cs 是 Razor Pages 的“一对”文件一个负责显示前端一个负责逻辑后端。比如前端点击按钮就会执行后端的代码而这后端代码有可以直接执行 业务逻辑EmployeeService的代码这样就不用访问api了当然你也可以访问api的写法去做。编写页面新建一个user文件夹里面写一个indexIndex.cshtmlpage model FurionRazorApi.Web.Entry.Pages.User.IndexModel { } h1员工管理/h1 h3新增员工/h3 form methodpost asp-page-handlerAdd div classrow div classcol-md-3 input typetext nameNewEmployee.Name classform-control placeholder姓名 required / /div div classcol-md-2 input typenumber nameNewEmployee.Age classform-control placeholder年龄 / /div div classcol-md-3 input typetext nameNewEmployee.Dept classform-control placeholder部门 / /div div classcol-md-2 button typesubmit classbtn btn-success新增员工/button /div /div /form hr / !-- 按钮提交表单触发 OnPost -- form methodpost button typesubmit classbtn btn-primary加载所有员工/button /form !-- 只有点击按钮后才显示表格 -- if (Model.HasData) { if (Model.EmployeeList ! null Model.EmployeeList.Any()) // ← 改成 EmployeeList { table classtable table-bordered table-striped thead tr thID/th th姓名/th th年龄/th th部门/th /tr /thead tbody foreach (var item in Model.EmployeeList) // ← 改成 EmployeeList { tr tditem.Id/td tditem.Name/td tditem.Age/td tditem.Dept/td /tr } /tbody /table } else { p暂无员工数据/p } }Index.cshtml.csusing FurionRazorApi.Application; using FurionRazorApi.Core.Models; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; namespace FurionRazorApi.Web.Entry.Pages.User { public class IndexModel : PageModel { private readonly IEmployeeService _employeeService; public ListEmployeeDto EmployeeList { get; set; } public string Message { get; set; } public bool HasData { get; set; } [BindProperty] public EmployeeDto NewEmployee { get; set; } public IndexModel(IEmployeeService employeeService) { _employeeService employeeService; } // 页面加载时显示空白 public void OnGet() { EmployeeList new ListEmployeeDto(); HasData false; Message 点击按钮加载员工数据; } // 点击按钮时加载所有员工 public async Task OnPost() { EmployeeList await _employeeService.GetEmoloyeesAsync(); HasData true; Message $共 {EmployeeList?.Count ?? 0} 名员工; } public async Task OnPostAdd() { if (string.IsNullOrWhiteSpace(NewEmployee?.Name)) { Message ❌ 员工姓名不能为空; EmployeeList await _employeeService.GetEmoloyeesAsync(); HasData true; return; } var success await _employeeService.CreateEmployeeAsync(NewEmployee); if (success!0) { Message $✅ 员工 {NewEmployee.Name} 添加成功; NewEmployee new EmployeeDto(); // 清空表单 } else { Message ❌ 添加失败; } EmployeeList await _employeeService.GetEmoloyeesAsync(); HasData true; } } }运行