ASP.NET Core 中的 Razor Pages

这篇具有很好参考价值的文章主要介绍了ASP.NET Core 中的 Razor Pages。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

Razor Pages

Razor Pages 是基于页面的 ASP.NET Core Web App 架构。
相比 MVC 模式,Razor Pages的生产效率更快。
Razer Pages 需要两个中间件:

  • builder…Services.AddRazorPages 添加 Razor Pages services
  • app.MapRazorPages 添加 Razor Pages endpoints

.cshtml 与 .cshtml.cs

在最简单的页面中:

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

看起来与 MVC 的页面差不多,但特别之处是有一个 @page 指令,@page 指令意味着这个页面可以直接接收 http request,而不需要通过 controller。

第2个页面Pages/Index2.cshtml 的代码是这样的:

@page
@using RazorPagesIntro.Pages
@model Index2Model

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

使用了 @using RazorPagesIntro.Pages 指令,
而RazorPagesIntro.Pages的实现代码Pages/Index2.cshtml.cs是这样的:

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

namespace RazorPagesIntro.Pages
{
    public class Index2Model : PageModel
    {
        public string Message { get; private set; } = "PageModel in C#";
        public void OnGet()
        {
            Message += $" Server time is { DateTime.Now }";
        }
    }
}

一个Index2.cshtml 页面,搭配一个Index2.cshtml.cs,类似WPF 中的 xaml与 xaml.cs。

URL Path 路由

url 的 Path 与页面的 path 相匹配。比如:

  • / or /Index 匹配 /Pages/Index.cshtml
  • /Contact 匹配 /Pages/Contact.cshtml
  • /Store/Contact 匹配 /Pages/Store/Contact.cshtml

一个 Post 例子

有一个Pages/Customers/Create.cshtml 的 view 页面,代码如下:
@model 指令中的CreateModel 对应一个名为 Create 的 Model,
而 Form 的 submit 会发生 Http Post,

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

<p>Enter a customer name:</p>

<form method="post">
    Name:
    <input asp-for="Customer!.Name" />
    <input type="submit" />
</form>

相应的Pages/Customers/Create.cshtml.cs 的代码中的部分如下:
OnPostAsync 处理 cshtml 中的 form submit,
一般还会有一个OnGet方法处理Http Get 请求。
RedirectToPage 方法会重定向到 ./Index 路径。
[BindProperty] 注解属性是表示model binding。
Razor Pages 中的BindProperty 一般用于非 GET 的属性。

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

public async Task<IActionResult> OnPostAsync()
{
    if (!ModelState.IsValid)
    {
        return Page();
    }
    if (Customer != null) _context.Customer.Add(Customer);
    return RedirectToPage("./Index");
}

Home Page 的例子

Index.cshtml

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

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

其中的

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

使用asp-route-id 生成指向Edit页面的URL,URL 中包含 Contact id,比如:
https://localhost:5001/Edit/1
而delete方法的html

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

由 server 生成后的html是:

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

对应的 Model 的代码Index.cshtml.cs:

public class IndexModel : PageModel
{
    private readonly Data.CustomerDbContext _context;
    public IndexModel(Data.CustomerDbContext context)
    {
        _context = context;
    }

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

    public async Task OnGetAsync()
    {
        Customers = await _context.Customer.ToListAsync();
    }

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

        if (contact != null)
        {
            _context.Customer.Remove(contact);
            await _context.SaveChangesAsync();
        }

        return RedirectToPage();
    }
}

使用Layouts,Partial,模板和Tag Helpers

待更新文章来源地址https://www.toymoban.com/news/detail-701027.html

URL Generation

待更新

ViewData 属性

待更新

TempData 属性

待更新

到了这里,关于ASP.NET Core 中的 Razor Pages的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包赞助服务器费用

相关文章

  • .NET 8 Preview 4 中的 ASP.NET Core 更新

    .NET 8 Preview 4 中的 ASP.NET Core 更新

    作者:Daniel Roth - Principal Program Manager, ASP.NET 翻译:Alan Wang 排版:Alan Wang .NET 8 Preview 4 现已可用,并包括了许多对 ASP.NET Core 的新改进。 以下是本预览版本中的新内容摘要: Blazor 使用 Blazor 组件进行流式渲染 使用 Blazor SSR 处理表单提交 在 Blazor 中路由到命名元素 Webcil 为 Bl

    2024年02月12日
    浏览(14)
  • ASP.NET Core 中的 MVC架构

    ASP.NET Core 中的 MVC架构

    MVC架构把 App 按照逻辑分成三层: Controllers,接收 http request,配合 model,通过http response 返回 view,尽量不做别的事 Models, 负责业务逻辑,App 的状态,以及数据处理 Views,呈现 UI,如果UI 较复杂,应该使用View 组件, ViewModel, 或者 view 模板 Controller ASP.NET Core MVC 中的所有 Control

    2024年02月09日
    浏览(10)
  • ASP.NET Core 中的 Dependency injection

    依赖注入 (Dependency Injection,简称DI)是为了实现 各个类之间的依赖 的 控制反转 (Inversion of Control,简称IoC )。 ASP.NET Core 中的Controller 和 Service 或者其他类都支持依赖注入。 依赖注入术语中, Service 是一个为其他对象提供服务的类 **。 Service 不是一个Web Service,与Web Serv

    2024年02月11日
    浏览(11)
  • 脱离于ASP.NET 和Visual Studio编辑Razor脚本

    脱离于ASP.NET 和Visual Studio编辑Razor脚本

    Razor Pad是一个编辑Razor脚本的工具,脱离于ASP.NET 和Visual Studio。 github地址:GitHub - RazorPad/RazorPad: RazorPad is a quick and simple stand-alone editing environment that allows anyone (even non-developers) to author Razor templates. It is the Notepad for Razor. 如果在编译源码时出现:签名时出错: 未能对 binDebugapp

    2024年01月21日
    浏览(8)
  • ASP.NET Core 中的 wwwroot 文件夹

    ASP.NET Core 中的 wwwroot 文件夹

    在本文中,我将讨论 ASP.NET Core 应用程序中的 wwwroot 文件夹。请阅读我们之前讨论过ASP.NET Core 请求处理管道的文章。在本文的最后,您将了解 wwwroot 文件夹及其需求以及如何在 ASP.NET Core 应用程序中进行配置。 ASP.NET Core 中的 wwwroot 文件夹是什么? 默认情况下,ASP.NET Core 应用

    2024年02月04日
    浏览(11)
  • ASP.NET Core 中的两种 Web API

    ASP.NET Core 有两种创建 RESTful Web API 的方式: 基于 Controller,使用完整的基于ControllerBase的基类定义接口endpoints。 基于 Minimal APIs,使用Lambda表达式定义接口 endpoints。 基于 Controller 的 Web API 可以使用构造函数注入,或者属性注入,遵循面向对象模式。 基于 Minimal APIs 的 Web API 通

    2024年02月09日
    浏览(18)
  • 安全机密管理:Asp.Net Core中的本地敏感数据保护技巧

    安全机密管理:Asp.Net Core中的本地敏感数据保护技巧

    在我们开发过程中基本上不可或缺的用到一些敏感机密数据,比如 SQL 服务器的连接串或者是 OAuth2 的 Secret 等,这些敏感数据在代码中是不太安全的,我们不应该在源代码中存储密码和其他的敏感数据,一种推荐的方式是通过 Asp.Net Core 的 机密管理器 。 在 ASP.NET Core 中,机密

    2024年04月25日
    浏览(16)
  • [Asp.Net Core] 网站中的XSS跨站脚本攻击和防范

    [Asp.Net Core] 网站中的XSS跨站脚本攻击和防范

    漏洞说明: 跨站脚本攻击(Cross Site Scripting),为了不和层叠样式表(Cascading Style Sheets, CSS)的缩写混淆,故将跨站脚本攻击缩写为XSS。恶意攻击者往Web页面里插入恶意Web脚本代码(html、javascript、css等),当用户浏览该页面时,嵌入其中的Web脚本代码会被执行,从而达到恶意攻击

    2023年04月14日
    浏览(9)
  • Web SSH 的原理与在 ASP.NET Core SignalR 中的实现

    Web SSH 的原理与在 ASP.NET Core SignalR 中的实现

    有个项目,需要在前端有个管理终端可以 SSH 到主控机的终端,如果不考虑用户使用 vim 等需要在控制台内现实界面的软件的话,其实使用 Process 类型去启动相应程序就够了。而这次的需求则需要考虑用户会做相关设置。 这里用到的原理是伪终端。伪终端(pseudo terminal)是现

    2024年02月07日
    浏览(12)
  • ASP.NET和ASP.NET Core的区别

    ASP.NET和ASP.NET Core是两个不同的Web应用程序框架,它们都是由Microsoft开发的。ASP.NET是Microsoft推出的第一个Web应用程序框架,而ASP.NET Core是其最新版本。本文将介绍ASP.NET和ASP.NET Core的简介和区别。 ASP.NET的简介 ASP.NET是一个基于.NET框架的Web应用程序框架,它是Microsoft推出的第一

    2024年02月16日
    浏览(50)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包