ASP.NET Core 中的 Razor 页面介绍Introduction to Razor Pages in ASP.NET Core

作者:Rick AndersonRyan NowakBy Rick Anderson and Ryan Nowak

Razor 页面是 ASP.NET Core MVC 的一个新特性,它可以使基于页面的编码方式更简单高效。Razor Pages is a new aspect of ASP.NET Core MVC that makes coding page-focused scenarios easier and more productive.

若要查找使用模型视图控制器方法的教程,请参阅 ASP.NET Core MVC 入门If you're looking for a tutorial that uses the Model-View-Controller approach, see Get started with ASP.NET Core MVC.

本文档介绍 Razor 页面。This document provides an introduction to Razor Pages. 它并不是分步教程。It's not a step by step tutorial. 如果认为某些部分过于复杂,请参阅 Razor 页面入门If you find some of the sections too advanced, see Get started with Razor Pages. 有关 ASP.NET Core 的概述,请参阅 ASP.NET Core 简介For an overview of ASP.NET Core, see the Introduction to ASP.NET Core.

系统必备Prerequisites

创建 Razor Pages 项目Create a Razor Pages project

请参阅 Razor Pages 入门,获取关于如何创建 Razor Pages 项目的详细说明。See Get started with Razor Pages for detailed instructions on how to create a Razor Pages project.

Razor 页面Razor Pages

Startup.cs 中已启用 Razor 页面 :Razor Pages is enabled in Startup.cs:

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        // Includes support for Razor Pages and controllers.
        services.AddMvc();
    }

    public void Configure(IApplicationBuilder app)
    {
        app.UseMvc();
    }
}

请考虑一个基本页面:Consider a basic page:

@page

<h1>Hello, world!</h1>
<h2>The time on the server is @DateTime.Now</h2>

前面的代码与具有控制器和视图的 ASP.NET Core 应用中使用的 Razor 视图文件非常相似。The preceding code looks a lot like a Razor view file used in an ASP.NET Core app with controllers and views. 不同之处在于 @page 指令。What makes it different is the @page directive. @page 使文件转换为一个 MVC 操作 ,这意味着它将直接处理请求,而无需通过控制器处理。@page makes the file into an MVC action - which means that it handles requests directly, without going through a controller. @page 必须是页面上的第一个 Razor 指令。@page must be the first Razor directive on a page. @page 将影响其他 Razor 构造的行为。@page affects the behavior of other Razor constructs.

将在以下两个文件中显示使用 PageModel 类的类似页面。A similar page, using a PageModel class, is shown in the following two files. Pages/Index2.cshtml 文件:The Pages/Index2.cshtml file:

@page
@using RazorPagesIntro.Pages
@model IndexModel2

<h2>Separate page model</h2>
<p>
    @Model.Message
</p>

Pages/Index2.cshtml.cs 页面模型 :The Pages/Index2.cshtml.cs page model:

using Microsoft.AspNetCore.Mvc.RazorPages;
using System;

namespace RazorPagesIntro.Pages
{
    public class IndexModel2 : PageModel
    {
        public string Message { get; private set; } = "PageModel in C#";

        public void OnGet()
        {
            Message += $" Server time is { DateTime.Now }";
        }
    }
}

按照惯例,PageModel 类文件的名称与追加 .cs 的 Razor 页面文件名称相同。By convention, the PageModel class file has the same name as the Razor Page file with .cs appended. 例如,前面的 Razor 页面的名称为 Pages/Index2.cshtml 。For example, the previous Razor Page is Pages/Index2.cshtml. 包含 PageModel 类的文件的名称为 Pages/Index2.cshtml.cs 。The file containing the PageModel class is named Pages/Index2.cshtml.cs.

页面的 URL 路径的关联由页面在文件系统中的位置决定。The associations of URL paths to pages are determined by the page's location in the file system. 下表显示了 Razor 页面路径及匹配的 URL:The following table shows a Razor Page path and the matching URL:

文件名和路径File name and path 匹配的 URLmatching URL
/Pages/Index.cshtml/Pages/Index.cshtml //Index/ or /Index
/Pages/Contact.cshtml/Pages/Contact.cshtml /Contact
/Pages/Store/Contact.cshtml/Pages/Store/Contact.cshtml /Store/Contact
/Pages/Store/Index.cshtml/Pages/Store/Index.cshtml /Store/Store/Index/Store or /Store/Index

注意:Notes:

  • 默认情况下,运行时在“Pages”文件夹中查找 Razor 页面文件。 The runtime looks for Razor Pages files in the Pages folder by default.
  • URL 未包含页面时,Index 为默认页面。Index is the default page when a URL doesn't include a page.

编写基本窗体Write a basic form

由于 Razor 页面的设计,在构建应用时可轻松实施用于 Web 浏览器的常用模式。Razor Pages is designed to make common patterns used with web browsers easy to implement when building an app. 模型绑定标记帮助程序和 HTML 帮助程序均只可用于 Razor 页面类中定义的属性。 Model binding, Tag Helpers, and HTML helpers all just work with the properties defined in a Razor Page class. 请参考为 Contact 模型实现基本的“联系我们”窗体的页面:Consider a page that implements a basic "contact us" form for the Contact model:

在本文档中的示例中,DbContextStartup.cs 文件中进行初始化。For the samples in this document, the DbContext is initialized in the Startup.cs file.

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using RazorPagesContacts.Data;

namespace RazorPagesContacts
{
    public class Startup
    {
        public IHostingEnvironment HostingEnvironment { get; }

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext<AppDbContext>(options =>
                              options.UseInMemoryDatabase("name"));
            services.AddMvc();
        }

        public void Configure(IApplicationBuilder app)
        {
            app.UseMvc();
        }
    }
}

数据模型:The data model:

using System.ComponentModel.DataAnnotations;

namespace RazorPagesContacts.Data
{
    public class Customer
    {
        public int Id { get; set; }

        [Required, StringLength(100)]
        public string Name { get; set; }
    }
}

数据库上下文:The db context:

using Microsoft.EntityFrameworkCore;

namespace RazorPagesContacts.Data
{
    public class AppDbContext : DbContext
    {
        public AppDbContext(DbContextOptions options)
            : base(options)
        {
        }

        public DbSet<Customer> Customers { get; set; }
    }
}

Pages/Create.cshtml 视图文件:The Pages/Create.cshtml view file:

@page
@model RazorPagesContacts.Pages.CreateModel
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

<html>
<body>
    <p>
        Enter your name.
    </p>
    <div asp-validation-summary="All"></div>
    <form method="POST">
        <div>Name: <input asp-for="Customer.Name" /></div>
        <input type="submit" />
    </form>
</body>
</html>

Pages/Create.cshtml.cs 页面模型 :The Pages/Create.cshtml.cs page model:

using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using RazorPagesContacts.Data;

namespace RazorPagesContacts.Pages
{
    public class CreateModel : PageModel
    {
        private readonly AppDbContext _db;

        public CreateModel(AppDbContext db)
        {
            _db = db;
        }

        [BindProperty]
        public Customer Customer { get; set; }

        public async Task<IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return Page();
            }

            _db.Customers.Add(Customer);
            await _db.SaveChangesAsync();
            return RedirectToPage("/Index");
        }
    }
}

按照惯例,PageModel 类命名为 <PageName>Model并且它与页面位于同一个命名空间中。By convention, the PageModel class is called <PageName>Model and is in the same namespace as the page.

使用 PageModel 类,可以将页面的逻辑与其展示分离开来。The PageModel class allows separation of the logic of a page from its presentation. 它定义了页面处理程序,用于处理发送到页面的请求和用于呈现页面的数据。It defines page handlers for requests sent to the page and the data used to render the page. 借助这种分离,可以通过依赖关系注入管理页面依赖关系,并对页面执行单元测试This separation allows you to manage page dependencies through dependency injection and to unit test the pages.

页面包含 OnPostAsync 处理程序方法 ,它在 POST 请求上运行(当用户发布窗体时)。The page has an OnPostAsync handler method, which runs on POST requests (when a user posts the form). 可以为任何 HTTP 谓词添加处理程序方法。You can add handler methods for any HTTP verb. 最常见的处理程序是:The most common handlers are:

  • OnGet,用于初始化页面所需的状态。OnGet to initialize state needed for the page. OnGet 示例。OnGet sample.
  • OnPost,用于处理窗体提交。OnPost to handle form submissions.

Async 命名后缀为可选,但是按照惯例通常会将它用于异步函数。The Async naming suffix is optional but is often used by convention for asynchronous functions. 前面示例中的 OnPostAsync 代码看上去与通常在控制器中编写的内容相似。The OnPostAsync code in the preceding example looks similar to what you would normally write in a controller. 前面的代码通常用于 Razor 页面。The preceding code is typical for Razor Pages. 多数 MVC 基元(例如模型绑定验证和操作结果)都是共享的。Most of the MVC primitives like model binding, validation, and action results are shared.

之前的 OnPostAsync 方法:The previous OnPostAsync method:

public async Task<IActionResult> OnPostAsync()
{
    if (!ModelState.IsValid)
    {
        return Page();
    }

    _db.Customers.Add(Customer);
    await _db.SaveChangesAsync();
    return RedirectToPage("/Index");
}

OnPostAsync 的基本流:The basic flow of OnPostAsync:

检查验证错误。Check for validation errors.

  • 如果没有错误,则保存数据并重定向。If there are no errors, save the data and redirect.
  • 如果有错误,则再次显示页面并附带验证消息。If there are errors, show the page again with validation messages. 客户端验证与传统的 ASP.NET Core MVC 应用程序相同。Client-side validation is identical to traditional ASP.NET Core MVC applications. 很多情况下,都会在客户端上检测到验证错误,并且从不将它们提交到服务器。In many cases, validation errors would be detected on the client, and never submitted to the server.

成功输入数据后,OnPostAsync 处理程序方法调用 RedirectToPage 帮助程序方法来返回 RedirectToPageResult 的实例。When the data is entered successfully, the OnPostAsync handler method calls the RedirectToPage helper method to return an instance of RedirectToPageResult. RedirectToPage 是新的操作结果,类似于 RedirectToActionRedirectToRoute,但是已针对页面进行自定义。RedirectToPage is a new action result, similar to RedirectToAction or RedirectToRoute, but customized for pages. 在前面的示例中,它将重定向到根索引页 (/Index)。In the preceding sample, it redirects to the root Index page (/Index). 页面 URL 生成部分中详细介绍了 RedirectToPageRedirectToPage is detailed in the URL generation for Pages section.

提交的窗体存在(已传递到服务器的)验证错误时,OnPostAsync 处理程序方法调用 Page 帮助程序方法。When the submitted form has validation errors (that are passed to the server), theOnPostAsync handler method calls the Page helper method. Page 返回 PageResult 的实例。Page returns an instance of PageResult. 返回 Page 的过程与控制器中的操作返回 View 的过程相似。Returning Page is similar to how actions in controllers return View. PageResult 是处理程序方法的PageResult is the default 默认返回类型。return type for a handler method. 返回 void 的处理程序方法将显示页面。A handler method that returns void renders the page.

Customer 属性使用 [BindProperty] 特性来选择加入模型绑定。The Customer property uses [BindProperty] attribute to opt in to model binding.

public class CreateModel : PageModel
{
    private readonly AppDbContext _db;

    public CreateModel(AppDbContext db)
    {
        _db = db;
    }

    [BindProperty]
    public Customer Customer { get; set; }

    public async Task<IActionResult> OnPostAsync()
    {
        if (!ModelState.IsValid)
        {
            return Page();
        }

        _db.Customers.Add(Customer);
        await _db.SaveChangesAsync();
        return RedirectToPage("/Index");
    }
}

默认情况下,Razor 页面只绑定带有非 GET 谓词的属性。Razor Pages, by default, bind properties only with non-GET verbs. 绑定属性可以减少需要编写的代码量。Binding to properties can reduce the amount of code you have to write. 绑定通过使用相同的属性显示窗体字段 (<input asp-for="Customer.Name">) 来减少代码,并接受输入。Binding reduces code by using the same property to render form fields (<input asp-for="Customer.Name">) and accept the input.

警告

出于安全原因,必须选择绑定 GET 请求数据以对模型属性进行分页。For security reasons, you must opt in to binding GET request data to page model properties. 请在将用户输入映射到属性前对其进行验证。Verify user input before mapping it to properties. 当处理依赖查询字符串或路由值的方案时,选择加入 GET 绑定非常有用。Opting in to GET binding is useful when addressing scenarios which rely on query string or route values.

要将属性绑定在 GET 请求上,请将 [BindProperty] 特性的 SupportsGet 属性设置为 true: [BindProperty(SupportsGet = true)]To bind a property on GET requests, set the [BindProperty] attribute's SupportsGet property to true: [BindProperty(SupportsGet = true)]

主页 (Index.cshtml ):The home page (Index.cshtml):

@page
@model RazorPagesContacts.Pages.IndexModel
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

<h1>Contacts</h1>
<form method="post">
    <table class="table">
        <thead>
            <tr>
                <th>ID</th>
                <th>Name</th>
            </tr>
        </thead>
        <tbody>
            @foreach (var contact in Model.Customers)
            {
                <tr>
                    <td>@contact.Id</td>
                    <td>@contact.Name</td>
                    <td>
                        <a asp-page="./Edit" asp-route-id="@contact.Id">edit</a>
                        <button type="submit" asp-page-handler="delete" 
                                asp-route-id="@contact.Id">delete</button>
                    </td>
                </tr>
            }
        </tbody>
    </table>

    <a asp-page="./Create">Create</a>
</form>

关联的 PageModel 类 (Index.cshtml.cs) :The associated PageModel class (Index.cshtml.cs):

using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using RazorPagesContacts.Data;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;

namespace RazorPagesContacts.Pages
{
    public class IndexModel : PageModel
    {
        private readonly AppDbContext _db;

        public IndexModel(AppDbContext db)
        {
            _db = db;
        }

        public IList<Customer> Customers { get; private set; }

        public async Task OnGetAsync()
        {
            Customers = await _db.Customers.AsNoTracking().ToListAsync();
        }

        public async Task<IActionResult> OnPostDeleteAsync(int id)
        {
            var contact = await _db.Customers.FindAsync(id);

            if (contact != null)
            {
                _db.Customers.Remove(contact);
                await _db.SaveChangesAsync();
            }

            return RedirectToPage();
        }
    }
}

Index.cshtml 文件包含以下标记来创建每个联系人项的编辑链接:The Index.cshtml file contains the following markup to create an edit link for each contact:

<a asp-page="./Edit" asp-route-id="@contact.Id">edit</a>

定位点标记帮助程序 使用 asp-route-{value} 属性生成“编辑”页面的链接。The Anchor Tag Helper used the asp-route-{value} attribute to generate a link to the Edit page. 此链接包含路由数据及联系人 ID。The link contains route data with the contact ID. 例如 http://localhost:5000/Edit/1For example, http://localhost:5000/Edit/1. 使用 asp-area 属性指定区域。Use the asp-area attribute to specify an area. 有关详细信息,请参阅 ASP.NET Core 中的区域For more information, see ASP.NET Core 中的区域.

Pages/Edit.cshtml 文件:The Pages/Edit.cshtml file:

@page "{id:int}"
@model RazorPagesContacts.Pages.EditModel
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

@{
    ViewData["Title"] = "Edit Customer";
}

<h1>Edit Customer - @Model.Customer.Id</h1>
<form method="post">
    <div asp-validation-summary="All"></div>
    <input asp-for="Customer.Id" type="hidden" />
    <div>
        <label asp-for="Customer.Name"></label>
        <div>
            <input asp-for="Customer.Name" />
            <span asp-validation-for="Customer.Name" ></span>
        </div>
    </div>
 
    <div>
        <button type="submit">Save</button>
    </div>
</form>

第一行包含 @page "{id:int}" 指令。The first line contains the @page "{id:int}" directive. 路由约束 "{id:int}" 告诉页面接受包含 int 路由数据的页面请求。The routing constraint"{id:int}" tells the page to accept requests to the page that contain int route data. 如果页面请求未包含可转换为 int 的路由数据,则运行时返回 HTTP 404(未找到)错误。If a request to the page doesn't contain route data that can be converted to an int, the runtime returns an HTTP 404 (not found) error. 若要使 ID 可选,请将 ? 追加到路由约束:To make the ID optional, append ? to the route constraint:

@page "{id:int?}"

Pages/Edit.cshtml.cs 文件:The Pages/Edit.cshtml.cs file:

using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.EntityFrameworkCore;
using RazorPagesContacts.Data;

namespace RazorPagesContacts.Pages
{
    public class EditModel : PageModel
    {
        private readonly AppDbContext _db;

        public EditModel(AppDbContext db)
        {
            _db = db;
        }

        [BindProperty]
        public Customer Customer { get; set; }

        public async Task<IActionResult> OnGetAsync(int id)
        {
            Customer = await _db.Customers.FindAsync(id);

            if (Customer == null)
            {
                return RedirectToPage("/Index");
            }

            return Page();
        }

        public async Task<IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return Page();
            }

            _db.Attach(Customer).State = EntityState.Modified;

            try
            {
                await _db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                throw new Exception($"Customer {Customer.Id} not found!");
            }

            return RedirectToPage("/Index");
        }
    }
}

Index.cshtml 文件还包含用于为每个客户联系人创建删除按钮的标记: The Index.cshtml file also contains markup to create a delete button for each customer contact:

<button type="submit" asp-page-handler="delete" 
        asp-route-id="@contact.Id">delete</button>

删除按钮采用 HTML 呈现,其 formaction 包括参数:When the delete button is rendered in HTML, its formaction includes parameters for:

  • asp-route-id 属性指定的客户联系人 ID。The customer contact ID specified by the asp-route-id attribute.
  • asp-page-handler 属性指定的 handlerThe handler specified by the asp-page-handler attribute.

下面是呈现的删除按钮的示例,其中客户联系人 ID 为 1Here is an example of a rendered delete button with a customer contact ID of 1:

<button type="submit" formaction="/?id=1&amp;handler=delete">delete</button>

选中按钮时,向服务器发送窗体 POST 请求。When the button is selected, a form POST request is sent to the server. 按照惯例,根据方案 OnPost[handler]Async 基于 handler 参数的值来选择处理程序方法的名称。By convention, the name of the handler method is selected based on the value of the handler parameter according to the scheme OnPost[handler]Async.

因为本示例中 handlerdelete,因此 OnPostDeleteAsync 处理程序方法用于处理 POST 请求。Because the handler is delete in this example, the OnPostDeleteAsync handler method is used to process the POST request. 如果 asp-page-handler 设置为其他值(如 remove),则选择名称为 OnPostRemoveAsync 的处理程序方法。If the asp-page-handler is set to a different value, such as remove, a handler method with the name OnPostRemoveAsync is selected.

public async Task<IActionResult> OnPostDeleteAsync(int id)
{
    var contact = await _db.Customers.FindAsync(id);

    if (contact != null)
    {
        _db.Customers.Remove(contact);
        await _db.SaveChangesAsync();
    }

    return RedirectToPage();
}

OnPostDeleteAsync 方法:The OnPostDeleteAsync method:

  • 接受来自查询字符串的 idAccepts the id from the query string.
  • 使用 FindAsync 查询客户联系人的数据库。Queries the database for the customer contact with FindAsync.
  • 如果找到客户联系人,则从客户联系人列表将其删除。If the customer contact is found, they're removed from the list of customer contacts. 数据库将更新。The database is updated.
  • 调用 RedirectToPage,重定向到根索引页 (/Index)。Calls RedirectToPage to redirect to the root Index page (/Index).

按需标记页面属性Mark page properties as required

PageModel 上的属性可通过 Required 特性进行修饰:Properties on a PageModel can be decorated with the Required attribute:

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System.ComponentModel.DataAnnotations;

namespace RazorPagesMovie.Pages.Movies
{
    public class CreateModel : PageModel
    {
        public IActionResult OnGet()
        {
            return Page();
        }

        [BindProperty]
        [Required(ErrorMessage = "Color is required")]
        public string Color { get; set; }

        public IActionResult OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return Page();
            }

            // Process color.

            return RedirectToPage("./Index");
        }
    }
}

有关详细信息,请参阅模型验证For more information, see Model validation.

使用 OnGet 处理程序回退来处理 HEAD 请求Handle HEAD requests with an OnGet handler fallback

HEAD 请求可检索特定资源的标头。HEAD requests allow you to retrieve the headers for a specific resource. GET 请求不同,HEAD 请求不返回响应正文。Unlike GET requests, HEAD requests don't return a response body.

通常,针对 HEAD 请求创建和调用 OnHead 处理程序:Ordinarily, an OnHead handler is created and called for HEAD requests:

public void OnHead()
{
    HttpContext.Response.Headers.Add("HandledBy", "Handled by OnHead!");
}

在 ASP.NET Core 2.1 或更高版本中,如果未定义 OnHead 处理程序,则 Razor Pages 会回退到调用 OnGet 处理程序。In ASP.NET Core 2.1 or later, Razor Pages falls back to calling the OnGet handler if no OnHead handler is defined. 此行为是通过在 Startup.ConfigureServices 中调用 SetCompatibilityVersion 而启用的:This behavior is enabled by the call to SetCompatibilityVersion in Startup.ConfigureServices:

services.AddMvc()
    .SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

默认模板在 ASP.NET Core 2.1 和 2.2 中生成 SetCompatibilityVersion 调用。The default templates generate the SetCompatibilityVersion call in ASP.NET Core 2.1 and 2.2. SetCompatibilityVersion 有效地将 Razor 页面选项 AllowMappingHeadRequestsToGetHandler 设置为 trueSetCompatibilityVersion effectively sets the Razor Pages option AllowMappingHeadRequestsToGetHandler to true.

可显式选择使用特定行为,而不是通过 SetCompatibilityVersion 选择使用所有行为 。Rather than opting in to all behaviors with SetCompatibilityVersion, you can explicitly opt in to specific behaviors. 通过下列代码,可选择允许将 HEAD 请求映射到 OnGet 处理程序:The following code opts in to allowing HEAD requests to be mapped to the OnGet handler:

services.AddMvc()
    .AddRazorPagesOptions(options =>
    {
        options.AllowMappingHeadRequestsToGetHandler = true;
    });

XSRF/CSRF 和 Razor 页面XSRF/CSRF and Razor Pages

无需为防伪验证编写任何代码。You don't have to write any code for antiforgery validation. Razor 页面自动将防伪标记生成过程和验证过程包含在内。Antiforgery token generation and validation are automatically included in Razor Pages.

将布局、分区、模板和标记帮助程序用于 Razor 页面Using Layouts, partials, templates, and Tag Helpers with Razor Pages

页面可使用 Razor 视图引擎的所有功能。Pages work with all the capabilities of the Razor view engine. 布局、分区、模板、标记帮助程序、_ViewStart.cshtml 和 _ViewImports.cshtml 的工作方式与它们在传统的 Razor 视图中的工作方式相同。Layouts, partials, templates, Tag Helpers, _ViewStart.cshtml, _ViewImports.cshtml work in the same way they do for conventional Razor views.

让我们使用其中的一些功能来整理此页面。Let's declutter this page by taking advantage of some of those capabilities.

向 Pages/Shared/_Layout.cshtml 添加布局页面Add a layout page to Pages/Shared/_Layout.cshtml:

向 Pages/_Layout.cshtml 添加布局页面Add a layout page to Pages/_Layout.cshtml:

<!DOCTYPE html>
<html>
<head> 
    <title>Razor Pages Sample</title>      
</head>
<body>    
   <a asp-page="/Index">Home</a>
    @RenderBody()  
    <a asp-page="/Customers/Create">Create</a> <br />
</body>
</html>

布局The Layout:

  • 控制每个页面的布局(页面选择退出布局时除外)。Controls the layout of each page (unless the page opts out of layout).
  • 导入 HTML 结构,例如 JavaScript 和样式表。Imports HTML structures such as JavaScript and stylesheets.

请参阅布局页面了解详细信息。See layout page for more information.

在 Pages/_ViewStart.cshtml 中设置 Layout 属性:The Layout property is set in Pages/_ViewStart.cshtml:

@{
    Layout = "_Layout";
}

布局位于“页面/共享”文件夹中。The layout is in the Pages/Shared folder. 页面按层次结构从当前页面的文件夹开始查找其他视图(布局、模板、分区)。Pages look for other views (layouts, templates, partials) hierarchically, starting in the same folder as the current page. 可以从“页面/共享” 文件夹下的任意 Razor 页面使用“页面” 文件夹中的布局。A layout in the Pages/Shared folder can be used from any Razor page under the Pages folder.

布局文件应位于 Pages/Shared 文件夹中。The layout file should go in the Pages/Shared folder.

布局位于“页面”文件夹中。The layout is in the Pages folder. 页面按层次结构从当前页面的文件夹开始查找其他视图(布局、模板、分区)。Pages look for other views (layouts, templates, partials) hierarchically, starting in the same folder as the current page. 可以从“页面” 文件夹下的任意 Razor 页面使用“页面” 文件夹中的布局。A layout in the Pages folder can be used from any Razor page under the Pages folder.

建议不要 将布局文件放在“视图/共享” 文件夹中。We recommend you not put the layout file in the Views/Shared folder. 视图/共享 是一种 MVC 视图模式。Views/Shared is an MVC views pattern. Razor 页面旨在依赖文件夹层次结构,而非路径约定。Razor Pages are meant to rely on folder hierarchy, not path conventions.

Razor 页面中的视图搜索包含“页面” 文件夹。View search from a Razor Page includes the Pages folder. 用于 MVC 控制器和传统 Razor 视图的布局、模板和分区可直接工作 。The layouts, templates, and partials you're using with MVC controllers and conventional Razor views just work.

添加 Pages/_ViewImports.cshtml 文件:Add a Pages/_ViewImports.cshtml file:

@namespace RazorPagesContacts.Pages
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

本教程的后续部分中将介绍 @namespace@namespace is explained later in the tutorial. @addTagHelper 指令将内置标记帮助程序引入“页面” 文件夹中的所有页面。The @addTagHelper directive brings in the built-in Tag Helpers to all the pages in the Pages folder.

页面上显式使用 @namespace 指令后:When the @namespace directive is used explicitly on a page:

@page
@namespace RazorPagesIntro.Pages.Customers

@model NameSpaceModel

<h2>Name space</h2>
<p>
    @Model.Message
</p>

此指令将为页面设置命名空间。The directive sets the namespace for the page. @model 指令无需包含命名空间。The @model directive doesn't need to include the namespace.

_ViewImports.cshtml 中包含 @namespace 指令后,指定的命名空间将为在导入 @namespace 指令的页面中生成的命名空间提供前缀。When the @namespace directive is contained in _ViewImports.cshtml, the specified namespace supplies the prefix for the generated namespace in the Page that imports the @namespace directive. 生成的命名空间的剩余部分(后缀部分)是包含 _ViewImports.cshtml 的文件夹与包含页面的文件夹之间以点分隔的相对路径。The rest of the generated namespace (the suffix portion) is the dot-separated relative path between the folder containing _ViewImports.cshtml and the folder containing the page.

例如,PageModel 类 Pages/Customers/Edit.cshtml.cs 显式设置命名空间:For example, the PageModel class Pages/Customers/Edit.cshtml.cs explicitly sets the namespace:

namespace RazorPagesContacts.Pages
{
    public class EditModel : PageModel
    {
        private readonly AppDbContext _db;

        public EditModel(AppDbContext db)
        {
            _db = db;
        }

        // Code removed for brevity.

Pages/_ViewImports.cshtml 文件设置以下命名空间:The Pages/_ViewImports.cshtml file sets the following namespace:

@namespace RazorPagesContacts.Pages
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

为 Pages/Customers/Edit.cshtml Razor 页面生成的命名空间与 PageModel 类相同。The generated namespace for the Pages/Customers/Edit.cshtml Razor Page is the same as the PageModel class.

@namespace 也可用于传统的 Razor 视图。@namespace also works with conventional Razor views.

原始的 Pages/Create.cshtml 视图文件:The original Pages/Create.cshtml view file:

@page
@model RazorPagesContacts.Pages.CreateModel
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

<html>
<body>
    <p>
        Enter your name.
    </p>
    <div asp-validation-summary="All"></div>
    <form method="POST">
        <div>Name: <input asp-for="Customer.Name" /></div>
        <input type="submit" />
    </form>
</body>
</html>

更新后的 Pages/Create.cshtml 视图文件:The updated Pages/Create.cshtml view file:

@page
@model CreateModel

<html>
<body>
    <p>
        Enter your name.
    </p>
    <div asp-validation-summary="All"></div>
    <form method="POST">
        <div>Name: <input asp-for="Customer.Name" /></div>
        <input type="submit" />
    </form>
</body>
</html>

Razor 页面初学者项目包含 Pages/_ValidationScriptsPartial.cshtml ,它与客户端验证联合。The Razor Pages starter project contains the Pages/_ValidationScriptsPartial.cshtml, which hooks up client-side validation.

有关分部视图的详细信息,请参阅 ASP.NET Core 中的分部视图For more information on partial views, see ASP.NET Core 中的分部视图.

页面的 URL 生成URL generation for Pages

之前显示的 Create 页面使用 RedirectToPageThe Create page, shown previously, uses RedirectToPage:

public async Task<IActionResult> OnPostAsync()
{
    if (!ModelState.IsValid)
    {
        return Page();
    }

    _db.Customers.Add(Customer);
    await _db.SaveChangesAsync();
    return RedirectToPage("/Index");
}

应用具有以下文件/文件夹结构:The app has the following file/folder structure:

  • /Pages /Pages

    • Index.cshtmlIndex.cshtml

    • /Customers /Customers

      • Create.cshtml Create.cshtml
      • Edit.cshtml Edit.cshtml
      • Index.cshtmlIndex.cshtml

成功后,Pages/Customers/Create.cshtml 和 Pages/Customers/Edit.cshtml 页面将重定向到 Pages/Index.cshtml 。The Pages/Customers/Create.cshtml and Pages/Customers/Edit.cshtml pages redirect to Pages/Index.cshtml after success. 字符串 /Index 是用于访问上一页的 URI 的组成部分。The string /Index is part of the URI to access the preceding page. 可以使用字符串 /Index 生成 Pages/Index.cshtml 页面的 URI。The string /Index can be used to generate URIs to the Pages/Index.cshtml page. 例如:For example:

  • Url.Page("/Index", ...)
  • <a asp-page="/Index">My Index Page</a>
  • RedirectToPage("/Index")

页面名称是从根“/Pages” 文件夹到页面的路径(包含前导 /,例如 /Index)。The page name is the path to the page from the root /Pages folder including a leading / (for example, /Index). 与硬编码 URL 相比,前面的 URL 生成示例提供了改进的选项和功能。The preceding URL generation samples offer enhanced options and functional capabilities over hardcoding a URL. URL 生成使用路由,并且可以根据目标路径定义路由的方式生成参数并对参数编码。URL generation uses routing and can generate and encode parameters according to how the route is defined in the destination path.

页面的 URL 生成支持相对名称。URL generation for pages supports relative names. 下表显示了 Pages/Customers/Create.cshtml 中不同的 RedirectToPage 参数选择的索引页:The following table shows which Index page is selected with different RedirectToPage parameters from Pages/Customers/Create.cshtml:

RedirectToPage(x)RedirectToPage(x) 页面Page
RedirectToPage("/Index")RedirectToPage("/Index") Pages/IndexPages/Index
RedirectToPage("./Index");RedirectToPage("./Index"); Pages/Customers/IndexPages/Customers/Index
RedirectToPage("../Index")RedirectToPage("../Index") Pages/IndexPages/Index
RedirectToPage("Index")RedirectToPage("Index") Pages/Customers/IndexPages/Customers/Index

RedirectToPage("Index")RedirectToPage("./Index")RedirectToPage("../Index") 是相对名称 。RedirectToPage("Index"), RedirectToPage("./Index"), and RedirectToPage("../Index") are relative names. 结合 RedirectToPage 参数与当前页的路径来计算目标页面的名称。The RedirectToPage parameter is combined with the path of the current page to compute the name of the destination page.

构建结构复杂的站点时,相对名称链接很有用。Relative name linking is useful when building sites with a complex structure. 如果使用相对名称链接文件夹中的页面,则可以重命名该文件夹。If you use relative names to link between pages in a folder, you can rename that folder. 所有链接仍然有效(因为这些链接未包含此文件夹名称)。All the links still work (because they didn't include the folder name).

若要重定向到不同区域中的页面,请指定区域:To redirect to a page in a different Area, specify the area:

RedirectToPage("/Index", new { area = "Services" });

有关详细信息,请参阅 ASP.NET Core 中的区域For more information, see ASP.NET Core 中的区域.

ViewData 特性ViewData attribute

可以通过 ViewDataAttribute 将数据传递到页面。Data can be passed to a page with ViewDataAttribute. 控制器或 Razor 页面模型上使用 [ViewData] 修饰的属性将其值存储在 ViewDataDictionary 中并从此处进行加载。Properties on controllers or Razor Page models decorated with [ViewData] have their values stored and loaded from the ViewDataDictionary.

在下面的示例中,AboutModel 包含使用 [ViewData] 修饰的 Title 属性。In the following example, the AboutModel contains a Title property decorated with [ViewData]. Title 属性设置为“关于”页面的标题:The Title property is set to the title of the About page:

public class AboutModel : PageModel
{
    [ViewData]
    public string Title { get; } = "About";

    public void OnGet()
    {
    }
}

在“关于”页面中,以模型属性的形式访问 Title 属性:In the About page, access the Title property as a model property:

<h1>@Model.Title</h1>

在布局中,从 ViewData 字典读取标题:In the layout, the title is read from the ViewData dictionary:

<!DOCTYPE html>
<html lang="en">
<head>
    <title>@ViewData["Title"] - WebApplication</title>
    ...

TempDataTempData

ASP.NET 在控制器上公开了 TempData 属性。ASP.NET Core exposes the TempData property on a controller. 此属性存储未读取的数据。This property stores data until it's read. KeepPeek 方法可用于检查数据,而不执行删除。The Keep and Peek methods can be used to examine the data without deletion. 多个请求需要数据时,TempData 有助于进行重定向。TempData is useful for redirection, when data is needed for more than a single request.

[TempData] 是 ASP.NET Core 2.0 中的新属性,在控制器和页面上受支持。The [TempData] attribute is new in ASP.NET Core 2.0 and is supported on controllers and pages.

下面的代码使用 TempData 设置 Message 的值:The following code sets the value of Message using TempData:

public class CreateDotModel : PageModel
{
    private readonly AppDbContext _db;

    public CreateDotModel(AppDbContext db)
    {
        _db = db;
    }

    [TempData]
    public string Message { get; set; }

    [BindProperty]
    public Customer Customer { get; set; }

    public async Task<IActionResult> OnPostAsync()
    {
        if (!ModelState.IsValid)
        {
            return Page();
        }

        _db.Customers.Add(Customer);
        await _db.SaveChangesAsync();
        Message = $"Customer {Customer.Name} added";
        return RedirectToPage("./Index");
    }
}

Pages/Customers/Index.cshtml 文件中的以下标记使用 TempData 显示 Message 的值。The following markup in the Pages/Customers/Index.cshtml file displays the value of Message using TempData.

<h3>Msg: @Model.Message</h3>

Pages/Customers/Index.cshtml.cs 页面模型将 [TempData] 属性应用到 Message 属性 。The Pages/Customers/Index.cshtml.cs page model applies the [TempData] attribute to the Message property.

[TempData]
public string Message { get; set; }

有关详细信息,请参阅 TempDataFor more information, see TempData .

针对一个页面的多个处理程序Multiple handlers per page

以下页面使用 asp-page-handler 标记帮助程序为两个处理程序生成标记:The following page generates markup for two handlers using the asp-page-handler Tag Helper:

@page
@model CreateFATHModel

<html>
<body>
    <p>
        Enter your name.
    </p>
    <div asp-validation-summary="All"></div>
    <form method="POST">
        <div>Name: <input asp-for="Customer.Name" /></div>
        <input type="submit" asp-page-handler="JoinList" value="Join" />
        <input type="submit" asp-page-handler="JoinListUC" value="JOIN UC" />
    </form>
</body>
</html>

前面示例中的窗体包含两个提交按钮,每个提交按钮均使用 FormActionTagHelper 提交到不同的 URL。The form in the preceding example has two submit buttons, each using the FormActionTagHelper to submit to a different URL. asp-page-handlerasp-page 的配套属性。The asp-page-handler attribute is a companion to asp-page. asp-page-handler 生成提交到页面定义的各个处理程序方法的 URL。asp-page-handler generates URLs that submit to each of the handler methods defined by a page. 未指定 asp-page,因为示例已链接到当前页面。asp-page isn't specified because the sample is linking to the current page.

页面模型:The page model:

using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using RazorPagesContacts.Data;

namespace RazorPagesContacts.Pages.Customers
{
    public class CreateFATHModel : PageModel
    {
        private readonly AppDbContext _db;

        public CreateFATHModel(AppDbContext db)
        {
            _db = db;
        }

        [BindProperty]
        public Customer Customer { get; set; }

        public async Task<IActionResult> OnPostJoinListAsync()
        {
            if (!ModelState.IsValid)
            {
                return Page();
            }

            _db.Customers.Add(Customer);
            await _db.SaveChangesAsync();
            return RedirectToPage("/Index");
        }

        public async Task<IActionResult> OnPostJoinListUCAsync()
        {
            if (!ModelState.IsValid)
            {
                return Page();
            }
            Customer.Name = Customer.Name?.ToUpper();
            return await OnPostJoinListAsync();
        }
    }
}

前面的代码使用已命名处理程序方法 。The preceding code uses named handler methods. 已命名处理程序方法通过采用名称中 On<HTTP Verb> 之后及 Async 之前的文本(如果有)创建。Named handler methods are created by taking the text in the name after On<HTTP Verb> and before Async (if present). 在前面的示例中,页面方法是 OnPostJoinListAsync 和 OnPostJoinListUCAsync。In the preceding example, the page methods are OnPostJoinListAsync and OnPostJoinListUCAsync. 删除 OnPost 和 Async 后,处理程序名称为 JoinListJoinListUCWith OnPost and Async removed, the handler names are JoinList and JoinListUC.

<input type="submit" asp-page-handler="JoinList" value="Join" />
<input type="submit" asp-page-handler="JoinListUC" value="JOIN UC" />

使用前面的代码时,提交到 OnPostJoinListAsync 的 URL 路径为 http://localhost:5000/Customers/CreateFATH?handler=JoinListUsing the preceding code, the URL path that submits to OnPostJoinListAsync is http://localhost:5000/Customers/CreateFATH?handler=JoinList. 提交到 OnPostJoinListUCAsync 的 URL 路径为 http://localhost:5000/Customers/CreateFATH?handler=JoinListUCThe URL path that submits to OnPostJoinListUCAsync is http://localhost:5000/Customers/CreateFATH?handler=JoinListUC.

自定义路由Custom routes

使用 @page 指令,执行以下操作:Use the @page directive to:

  • 指定页面的自定义路由。Specify a custom route to a page. 例如,使用 @page "/Some/Other/Path" 将“关于”页面的路由设置为 /Some/Other/PathFor example, the route to the About page can be set to /Some/Other/Path with @page "/Some/Other/Path".
  • 将段追加到页面的默认路由。Append segments to a page's default route. 例如,可使用 @page "item" 将“item”段添加到页面的默认路由。For example, an "item" segment can be added to a page's default route with @page "item".
  • 将参数追加到页面的默认路由。Append parameters to a page's default route. 例如,@page "{id}" 页面需要 ID 参数 idFor example, an ID parameter, id, can be required for a page with @page "{id}".

支持开头处以波形符 (~) 指定的相对于根目录的路径。A root-relative path designated by a tilde (~) at the beginning of the path is supported. 例如,@page "~/Some/Other/Path"@page "/Some/Other/Path" 相同。For example, @page "~/Some/Other/Path" is the same as @page "/Some/Other/Path".

可以通过指定路由模板 @page "{handler?}",将 URL 中的查询字符串 ?handler=JoinList 更改为路由段 /JoinListYou can change the query string ?handler=JoinList in the URL to a route segment /JoinList by specifying the route template @page "{handler?}".

如果你不喜欢 URL 中的查询字符串 ?handler=JoinList,可以更改路由,将处理程序名称放在 URL 的路径部分。If you don't like the query string ?handler=JoinList in the URL, you can change the route to put the handler name in the path portion of the URL. 可以通过在 @page 指令后面添加使用双引号括起来的路由模板来自定义路由。You can customize the route by adding a route template enclosed in double quotes after the @page directive.

@page "{handler?}"
@model CreateRouteModel

<html>
<body>
    <p>
        Enter your name.
    </p>
    <div asp-validation-summary="All"></div>
    <form method="POST">
        <div>Name: <input asp-for="Customer.Name" /></div>
        <input type="submit" asp-page-handler="JoinList" value="Join" />
        <input type="submit" asp-page-handler="JoinListUC" value="JOIN UC" />
    </form>
</body>
</html>

使用前面的代码时,提交到 OnPostJoinListAsync 的 URL 路径为 http://localhost:5000/Customers/CreateFATH/JoinListUsing the preceding code, the URL path that submits to OnPostJoinListAsync is http://localhost:5000/Customers/CreateFATH/JoinList. 提交到 OnPostJoinListUCAsync 的 URL 路径为 http://localhost:5000/Customers/CreateFATH/JoinListUCThe URL path that submits to OnPostJoinListUCAsync is http://localhost:5000/Customers/CreateFATH/JoinListUC.

handler 前面的 ? 表示路由参数为可选。The ? following handler means the route parameter is optional.

配置和设置Configuration and settings

若要配置高级选项,请在 MVC 生成器上使用 AddRazorPagesOptions 扩展方法:To configure advanced options, use the extension method AddRazorPagesOptions on the MVC builder:

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc()
        .AddRazorPagesOptions(options =>
        {
            options.RootDirectory = "/MyPages";
            options.Conventions.AuthorizeFolder("/MyPages/Admin");
        });
}

目前,可以使用 RazorPagesOptions 设置页面的根目录,或者为页面添加应用程序模型约定。Currently you can use the RazorPagesOptions to set the root directory for pages, or add application model conventions for pages. 通过这种方式,我们在将来会实现更多扩展功能。We'll enable more extensibility this way in the future.

若要预编译视图,请参阅 Razor 视图编译To precompile views, see Razor view compilation .

下载或查看示例代码.Download or view sample code.

请参阅 Razor 页面入门,这篇文章以本文为基础编写。See Get started with Razor Pages, which builds on this introduction.

指定 Razor 页面位于内容根目录中Specify that Razor Pages are at the content root

默认情况下,Razor 页面位于 /Pages 目录的根位置 。By default, Razor Pages are rooted in the /Pages directory. AddMvc 添加 WithRazorPagesAtContentRoot,以指定 Razor 页面位于应用的内容根目录 (ContentRootPath) 中:Add WithRazorPagesAtContentRoot to AddMvc to specify that your Razor Pages are at the content root (ContentRootPath) of the app:

services.AddMvc()
    .AddRazorPagesOptions(options =>
    {
        ...
    })
    .WithRazorPagesAtContentRoot();

指定 Razor 页面位于自定义根目录中Specify that Razor Pages are at a custom root directory

AddMvc 添加 WithRazorPagesRoot,以指定 Razor 页面位于应用中自定义根目录位置(提供相对路径):Add WithRazorPagesRoot to AddMvc to specify that your Razor Pages are at a custom root directory in the app (provide a relative path):

services.AddMvc()
    .AddRazorPagesOptions(options =>
    {
        ...
    })
    .WithRazorPagesRoot("/path/to/razor/pages");

其他资源Additional resources