Razor Pages与服务器端Blazor [英] Razor Pages vs server-side Blazor

查看:580
本文介绍了Razor Pages与服务器端Blazor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

就像过去一样,Razor Pages 用于服务器端Web应用程序.

Razor Pages is used for server side web applications, just like in the good old days.

Blazor 旨在提供一种流行的JavaScript框架(例如Angular或React)的替代方法,以创建主要在客户端浏览器中运行的单页应用程序(SPA).

Blazor is aiming to provide an alternative to popular JavaScript frameworks (like Angular or React), to create Single Page Applications (SPAs) which runs (mainly) in the clients browser.

但是,我也听说过服务器端Blazor,这使我感到困惑.根据此答案,服务器端Blazor只是服务器上运行的Razor组件.但是Razor Pages和Razor Components之间有什么区别?

However, I have also heard about server-side Blazor, which kind of confuses me. According to this answer, server side Blazor is just Razor Components running on the server. But what is the difference between Razor Pages and Razor Components?

注意:我不是在试图找出哪个更好或者是正确的选择".我只是试图找出存在哪些技术差异.

Note: I am not trying to figure out which is better or "the right choice". I am simply trying to figure out which technical differences there are.

推荐答案

Razor Components的对象是用于创建网页的可重用组件.

Razor Components, as they are named, are for creating reusable components for web pages.

Razor pages是网页和控制器在单个文件中的组合.

Razor pages are the combination of a web page and a controller in a single file.

Razor components主要由Blazor使用,但它们也可以在Razor Pages中使用,尽管它们被设计为对Blazor更原生.

Razor components are primarily used by Blazor but they can also be used within Razor Pages, although they are designed to be more native to Blazor.

在没有页面托管的情况下不能显示Razor Component,但是在没有Razor components的情况下可以显示Razor Pages.

You can't display a Razor Component without a page to host it, but you can display Razor Pages without Razor components.

Razor Components可从.NET Core 3.0及更高版本开始使用.

Razor Components are available from .NET Core 3.0 onwards.

Razor Pages可从.NET Core 2.1开始使用.

Razor Pages are available from .NET Core 2.1 onwards.

编辑

RazorPages在HTML页面和.cs代码文件之间拆分.尽管Razor Components和HTML可以分为一个文件,但通常可以将.cs和HTML放在一个文件中.

RazorPages are split between an HTML page and a .cs code file. Whereas Razor Components usually have the .cs and HTML in a single file, although they can be separated into HTML and a Code Behind file.

Razor PagePageModel允许ASP.NET Core将控制器返回的数据绑定到页面中的公共属性,然后在页面中使用该属性引用模型.您可以使用PageModel类中的属性来引用代码中的数据,并使用HTML中的@model属性来引用相同的属性.

The PageModel for a Razor Page allows ASP.NET Core to bind the data returned by the controller to a public property in the page and then use that property within your page to reference the model. You use the property in the PageModel class to reference the data in the code and use the @model property within the HTML to reference the same properties.

Razor Components不会绑定到模型,而是使用参数为它们分配值,类似于将值和事件分配给标准HTML元素的方式.例如,可以看到这里.

Razor Components do not bind to a model but you assign values to them using parameters, similar to how you assign values and events to a standard HTML element. An example of this can be seen here.

这篇关于Razor Pages与服务器端Blazor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆