1. 修改Program //添加Razor页面 builder.Services.AddRazorPages(); //使用路由,需在app.UseAntiforgery();之前添加 app.UseRouting(); //映射Razor页面 app.MapRazorPages(); 2. 添加Razor页面 新建Pages文件夹在Pages文件夹中添加_ViewImports.cshtml文件 @using Sample.Web @namespace Sample.Web.Pages @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers 在Pages文件夹中添加Razor页面,命名Test,自动生成Test.cshtml和Test.cshtml.cs两个文件Test.cshtml文件示例如下: @page @model TestModel <h2>@Model.Id</h2> Test.cshtml.cs文件示例如下: namespace Sample.Web.Pages; public class TestModel : Microsoft.AspNetCore.Mvc.RazorPages.PageModel { public string Id { get; set; } //路由:域名/test?id=123242 public async void OnGet(string id) { Id = id; } }