本系列学习笔记均来源于B站UP主”软件工艺师“的学习视频,学习连接如下:
https://www.bilibili.com/video/BV1c441167KQ
ASP.NET Core 3.x 学习笔记(5)——Razor Page
Razor Page
- MVC,实现如下三个分层:
- Model:数据
- View:HTML、Razor、TagHelpers
- Controllers:逻辑
- Razor Page,将下列三个部分同意:
- 数据
- Html、Razor、TagHelpers
- 逻辑
Razor Page 路由
在 Razor Page 中,默认视图页面在 Pages 文件夹中,路由可省略为如下格式。
- /Pages/Index.cshtml
- /
- /Index
- /Pages/Department.cshtml
- /Department
- /Pages/Department/EmployeeList.cshtml
- /Department/EmployeeList
- /Pages/Department/Index.cshtml
- /Department
- /Department/Index
在前文中的 MVC 项目上简单更改成 Razor Page
本例直接复制 MVC 中的 Models、Services、css等即可,这部分代码没有任何改变,但是不需要 Controller 和 Views。在 Razor Page 中,基本是可以理解为,将 View 和 Controller 结合在 Pages 中。具体代码如下:
-
Startup.cs 稍有变动,需要添加 RazorPage 使用的东西
using ASPNETCore_Learning_ThreePage.Services; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace ASPNETCore_Learning_ThreePage { public class Startup { private readonly IConfiguration _configuration; //存储配置信息的键值对 public Startup(IConfiguration configuration) { this._configuration = configuration; } public void ConfigureServices(IServiceCollection services) { services.AddRazorPages(); //添加 RazorPage 使用的东西 services.AddSingleton<IDepartmentService, DepartmentService>(); services.AddSingleton<IEmployeeService, EmployeeService>(); } public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseStaticFiles(); app.UseHttpMethodOverride(); app.UseAuthentication(); app.UseRouting(); app.UseEndpoints(endpoints => { endpoints.MapRazorPages(); //更改为 Razor Page }); } } } -
建立母版页:建立方式与 MVC 基本一致,通过“Shared/_Layout.cshtml”、“_ViewStart.cshtml”设置,但在视图导入(_ViewImpotr.cshtml)中存在一定区别,如下:需要添加 @namespace ASPNETCore_Learning_ThreePage.Pages
@namespace ASPNETCore_Learning_ThreePage.Pages @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers -
建立 Razor Page 页面
Pages
Pages 即为 MVC 中 View 与 Controller 的结合。下面通过建立三个不同的页面,展示不同情况下 Razor Page 的使用。
-
建立 Department 列表页面的“Razor 视图”
@page @using ASPNETCore_Learning_ThreePage.Services @inject IDepartmentService departmentService <div class="row"> <div class="col-md-10 offset-md-2"> <table class="table"> <tr> <th>Name</th> <th>Location</th> <th>Employee</th> <th>操作</th> </tr> @Html.DisplayFor(x => x.Departments) </table> </div> </div> <div class="row"> <div class="com-md-4"> <a asp-page="Department/AddDepartment">Add</a> </div> </div> @*C#代码处理请求*@ @functions { public IEnumerable<ASPNETCore_Learning_ThreePage.Models.Department> Departments { get; set; } //get 请求在此处理 public async Task OnGetAsync() { Departments = await departmentService.GetAll(); } }- 视图中默认要添加 @page,其它与 MVC 中的 Razor 基本一致使用
- 处理请求的 C# 逻辑代码写在
@functions代码块中 - 请求方法页有统一的约定,如 OnGetAsync、OnGet、OnPostAsync
-
使用”Razor 页面“建立页面。创建后,除 .cshtml 文件外,还有 .cshtml.cs 文件。如下:

其中 .cshtml 文件进行页面展示, .cshtml.cs 文件进行逻辑处理,如下:
- 使用 ”[BindProperty]“ 绑定页面属性,使代码中不需要再另外处理 GET 请求;页面直接通过 “Department(对象).Name(属性)”可以直接获取绑定值
- 处理 Post 请求的代码基本和 MVC 中一致
AddDepartmentModel.cshtml.cs
using System.Threading.Tasks;
using ASPNETCore_Learning_ThreePage.Services;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace ASPNETCore_Learning_ThreePage.Pages.Department
{
public class AddDepartmentModel : PageModel
{
private readonly IDepartmentService _departmentService;
//通过BindProperty直接绑定,不需要另外处理 Get 请求
[BindProperty]
public ASPNETCore_Learning_ThreePage.Models.Department Department { get; set; }
public AddDepartmentModel(IDepartmentService departmentService)
{
_departmentService = departmentService;
}
//需要处理 Post 请求
public async Task<IActionResult> OnPostAsync()
{
if (!ModelState.IsValid)
{
//不合法直接返回当前页面,重新填写
return Page();
}
await _departmentService.Add(Department);
return RedirectToPage("/Index");
//return RedirectToPage("Index"); //会返回当前目录下的 Index 页面,即Department文件夹下的Index页面
}
}
}
AddDepartmentModel.cshtml
@page
@model ASPNETCore_Learning_ThreePage.Pages.Department.AddDepartmentModel
<form asp-action="Add">
<div class="row form-group">
<div class="col-md-2 offset-md-2">
<label asp-for="Department.Name"></label>
</div>
<div class="col-md-6">
<input class="form-control" asp-for="Department.Name" />
</div>
</div>
<div class="row form-group">
<div class="col-md-2 offset-md-2">
<label asp-for="Department.Location"></label>
</div>
<div class="col-md-6">
<input class="form-control" asp-for="Department.Location" />
</div>
</div>
<div class="row form-group">
<div class="col-md-2 offset-md-2">
<label asp-for="Department.EmployeeCount"></label>
</div>
<div class="col-md-6">
<input class="form-control" asp-for="Department.EmployeeCount" />
</div>
</div>
<div class="row">
<div class="col-md-2 offset-md-2">
<button type="submit" class="btn btn-primary">Add</button>
</div>
</div>
</form>
-
建立 “Razor 视图”,在视图中使用 @page 传递参数。如下:
@page "{departmentId:int}" @using Microsoft.AspNetCore.Mvc.RazorPages @using ASPNETCore_Learning_ThreePage.Services @using ASPNETCore_Learning_ThreePage.Models @model EmployeeListModel <div class="row"> <div class="col-md-10 offset-md-2"> <table class="table"> <tr> <th>First Name</th> <th>Last Name</th> <th>Gender</th> <th>Is Fired</th> <th>操作</th> </tr> @Html.DisplayFor(x => x.Employees) </table> </div> </div> <div class="row"> <div class="col-md-4 offset-md-2"> <a asp-action="Add" asp-route-departmentID="@ViewBag.DepartmentId">Add</a> </div> </div> @functions{ public class EmployeeListModel : PageModel { private readonly IEmployeeService _employeeService; public EmployeeListModel(IEmployeeService employeeService) { _employeeService = employeeService; } public IEnumerable<Employee> Employees { get; set; } public async Task OnGet(int departmentId) { Employees = await _employeeService.GetByDepartmentId(departmentId); ViewData["DepartmentId"] = departmentId; } } }注意:
- 逻辑代码要放在 @functions 中,否则编译会报错
通过杨老师的课程,对 Razor Page 的认知懵懵懂懂,后续找时间阅读 MSDN 补充笔记。
本文介绍了如何在ASP.NET Core 3.x项目中切换到RazorPage架构,讲解了路由、母版页设置、部门列表示例及添加功能的实现,展示了RazorPage如何结合数据、HTML/Razor和逻辑。
941

被折叠的 条评论
为什么被折叠?



