国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

ASP.NET Core 中的 Razor Pages

這篇具有很好參考價(jià)值的文章主要介紹了ASP.NET Core 中的 Razor Pages。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問(wèn)。

Razor Pages

Razor Pages 是基于頁(yè)面的 ASP.NET Core Web App 架構(gòu)。
相比 MVC 模式,Razor Pages的生產(chǎn)效率更快。
Razer Pages 需要兩個(gè)中間件:

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

.cshtml 與 .cshtml.cs

在最簡(jiǎn)單的頁(yè)面中:

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

看起來(lái)與 MVC 的頁(yè)面差不多,但特別之處是有一個(gè) @page 指令,@page 指令意味著這個(gè)頁(yè)面可以直接接收 http request,而不需要通過(guò) controller。

第2個(gè)頁(yè)面Pages/Index2.cshtml 的代碼是這樣的:

@page
@using RazorPagesIntro.Pages
@model Index2Model

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

使用了 @using RazorPagesIntro.Pages 指令,
而RazorPagesIntro.Pages的實(shí)現(xiàn)代碼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 }";
        }
    }
}

一個(gè)Index2.cshtml 頁(yè)面,搭配一個(gè)Index2.cshtml.cs,類似WPF 中的 xaml與 xaml.cs。

URL Path 路由

url 的 Path 與頁(yè)面的 path 相匹配。比如:

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

一個(gè) Post 例子

有一個(gè)Pages/Customers/Create.cshtml 的 view 頁(yè)面,代碼如下:
@model 指令中的CreateModel 對(duì)應(yīng)一個(gè)名為 Create 的 Model,
而 Form 的 submit 會(huì)發(fā)生 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>

相應(yīng)的Pages/Customers/Create.cshtml.cs 的代碼中的部分如下:
OnPostAsync 處理 cshtml 中的 form submit,
一般還會(huì)有一個(gè)OnGet方法處理Http Get 請(qǐng)求。
RedirectToPage 方法會(huì)重定向到 ./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頁(yè)面的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>

對(duì)應(yīng)的 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

待更新文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-701027.html

URL Generation

待更新

ViewData 屬性

待更新

TempData 屬性

待更新

到了這里,關(guān)于ASP.NET Core 中的 Razor Pages的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來(lái)自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • .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 現(xiàn)已可用,并包括了許多對(duì) ASP.NET Core 的新改進(jìn)。 以下是本預(yù)覽版本中的新內(nèi)容摘要: Blazor 使用 Blazor 組件進(jìn)行流式渲染 使用 Blazor SSR 處理表單提交 在 Blazor 中路由到命名元素 Webcil 為 Bl

    2024年02月12日
    瀏覽(24)
  • ASP.NET Core 中的 MVC架構(gòu)

    ASP.NET Core 中的 MVC架構(gòu)

    MVC架構(gòu)把 App 按照邏輯分成三層: Controllers,接收 http request,配合 model,通過(guò)http response 返回 view,盡量不做別的事 Models, 負(fù)責(zé)業(yè)務(wù)邏輯,App 的狀態(tài),以及數(shù)據(jù)處理 Views,呈現(xiàn) UI,如果UI 較復(fù)雜,應(yīng)該使用View 組件, ViewModel, 或者 view 模板 Controller ASP.NET Core MVC 中的所有 Control

    2024年02月09日
    瀏覽(19)
  • ASP.NET Core 中的 Dependency injection

    依賴注入 (Dependency Injection,簡(jiǎn)稱DI)是為了實(shí)現(xiàn) 各個(gè)類之間的依賴 的 控制反轉(zhuǎn) (Inversion of Control,簡(jiǎn)稱IoC )。 ASP.NET Core 中的Controller 和 Service 或者其他類都支持依賴注入。 依賴注入術(shù)語(yǔ)中, Service 是一個(gè)為其他對(duì)象提供服務(wù)的類 **。 Service 不是一個(gè)Web Service,與Web Serv

    2024年02月11日
    瀏覽(23)
  • 脫離于ASP.NET 和Visual Studio編輯Razor腳本

    脫離于ASP.NET 和Visual Studio編輯Razor腳本

    Razor Pad是一個(gè)編輯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. 如果在編譯源碼時(shí)出現(xiàn):簽名時(shí)出錯(cuò): 未能對(duì) binDebugapp

    2024年01月21日
    瀏覽(22)
  • ASP.NET Core 中的 wwwroot 文件夾

    ASP.NET Core 中的 wwwroot 文件夾

    在本文中,我將討論 ASP.NET Core 應(yīng)用程序中的 wwwroot 文件夾。請(qǐng)閱讀我們之前討論過(guò)ASP.NET Core 請(qǐng)求處理管道的文章。在本文的最后,您將了解 wwwroot 文件夾及其需求以及如何在 ASP.NET Core 應(yīng)用程序中進(jìn)行配置。 ASP.NET Core 中的 wwwroot 文件夾是什么? 默認(rèn)情況下,ASP.NET Core 應(yīng)用

    2024年02月04日
    瀏覽(27)
  • ASP.NET Core 中的兩種 Web API

    ASP.NET Core 有兩種創(chuàng)建 RESTful Web API 的方式: 基于 Controller,使用完整的基于ControllerBase的基類定義接口endpoints。 基于 Minimal APIs,使用Lambda表達(dá)式定義接口 endpoints。 基于 Controller 的 Web API 可以使用構(gòu)造函數(shù)注入,或者屬性注入,遵循面向?qū)ο竽J健?基于 Minimal APIs 的 Web API 通

    2024年02月09日
    瀏覽(34)
  • 安全機(jī)密管理:Asp.Net Core中的本地敏感數(shù)據(jù)保護(hù)技巧

    安全機(jī)密管理:Asp.Net Core中的本地敏感數(shù)據(jù)保護(hù)技巧

    在我們開(kāi)發(fā)過(guò)程中基本上不可或缺的用到一些敏感機(jī)密數(shù)據(jù),比如 SQL 服務(wù)器的連接串或者是 OAuth2 的 Secret 等,這些敏感數(shù)據(jù)在代碼中是不太安全的,我們不應(yīng)該在源代碼中存儲(chǔ)密碼和其他的敏感數(shù)據(jù),一種推薦的方式是通過(guò) Asp.Net Core 的 機(jī)密管理器 。 在 ASP.NET Core 中,機(jī)密

    2024年04月25日
    瀏覽(26)
  • [Asp.Net Core] 網(wǎng)站中的XSS跨站腳本攻擊和防范

    [Asp.Net Core] 網(wǎng)站中的XSS跨站腳本攻擊和防范

    漏洞說(shuō)明: 跨站腳本攻擊(Cross Site Scripting),為了不和層疊樣式表(Cascading Style Sheets, CSS)的縮寫(xiě)混淆,故將跨站腳本攻擊縮寫(xiě)為XSS。惡意攻擊者往Web頁(yè)面里插入惡意Web腳本代碼(html、javascript、css等),當(dāng)用戶瀏覽該頁(yè)面時(shí),嵌入其中的Web腳本代碼會(huì)被執(zhí)行,從而達(dá)到惡意攻擊

    2023年04月14日
    瀏覽(18)
  • Web SSH 的原理與在 ASP.NET Core SignalR 中的實(shí)現(xiàn)

    Web SSH 的原理與在 ASP.NET Core SignalR 中的實(shí)現(xiàn)

    有個(gè)項(xiàng)目,需要在前端有個(gè)管理終端可以 SSH 到主控機(jī)的終端,如果不考慮用戶使用 vim 等需要在控制臺(tái)內(nèi)現(xiàn)實(shí)界面的軟件的話,其實(shí)使用 Process 類型去啟動(dòng)相應(yīng)程序就夠了。而這次的需求則需要考慮用戶會(huì)做相關(guān)設(shè)置。 這里用到的原理是偽終端。偽終端(pseudo terminal)是現(xiàn)

    2024年02月07日
    瀏覽(20)
  • ASP.NET和ASP.NET Core的區(qū)別

    ASP.NET和ASP.NET Core是兩個(gè)不同的Web應(yīng)用程序框架,它們都是由Microsoft開(kāi)發(fā)的。ASP.NET是Microsoft推出的第一個(gè)Web應(yīng)用程序框架,而ASP.NET Core是其最新版本。本文將介紹ASP.NET和ASP.NET Core的簡(jiǎn)介和區(qū)別。 ASP.NET的簡(jiǎn)介 ASP.NET是一個(gè)基于.NET框架的Web應(yīng)用程序框架,它是Microsoft推出的第一

    2024年02月16日
    瀏覽(96)

覺(jué)得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包