.NET 9 - Static SSR pages in a globally-interactive app

1.简单介绍

.NET 9 Blazor 新增加的一个feature是在Interactive模式的Blazor站点中可以设定某个页面为Static SSR模式。

这边也简单尝试一下这个新的特性

2.具体说明

2.1 创建项目

  1. 创建一个Blazor Web Assembly的项目,

2)编辑App.razor

复制代码
<head>
	...
    <HeadOutlet @rendermode="@PageRenderMode" />
</head>
<body>
    <Routes @rendermode="@PageRenderMode" />
    ...
</body>
@code {
    [CascadingParameter]
    private HttpContext HttpContext { get; set; } = default!;

    private IComponentRenderMode? PageRenderMode
        => HttpContext.AcceptsInteractiveRouting() ? InteractiveWebAssembly : null;
}

3)把Weather.razor页面变成Static SSR,

复制代码
@page "/weather"
@attribute [ExcludeFromInteractiveRouting]

2.2 运行一下

发现Weather.razor页面能正常工作,同时页面是服务器端渲染之后传到浏览器的

这时如果是从Counter页面跳转到Weather页面,发现服务端会重新渲染并下载的Weather页面

如果Weather.razor页面没有设定 @attribute ExcludeFromInteractiveRouting

则Weather页面也是InterActiveWebAssemblyRenderMode的,和别的页面一样的render mode。测试了一下,发现这种情况下从Counter页面跳转到Weather页面是没有从服务器端重新下载Weather页面的。

3.总结

本文简单尝试了一下在全局设定为Interactive(可以为InteractiveServer,InteractiveWebAssembly,

InteractiveAuto)情况下,如何将其中一个页面变成Static SSR,同时看看和未变成Static SSR时候有什么不同的。

如果哪里有错误,麻烦告之,谢谢谢谢!

相关推荐
Scout-leaf1 小时前
C#摸鱼实录——IoC与DI案例详解
c#
咕白m6253 小时前
使用 C# 在 Excel 中应用多种字体样式
后端·c#
Artech8 小时前
[MAF预定义的AIContextProvider-02]AgentSkillsProvider——将Agent Skills引入MAF
ai·c#·agent·agent skills·maf
Data-Miner1 天前
大语言模型+智能体AI,122页PPT详解落地应用培训!
人工智能·microsoft·语言模型
2601_962072551 天前
李梦娇常识4600问|题库|打印版
sql·华为od·华为·c#·华为云·.net·harmonyos
m0_547486661 天前
《C#语言程序设计与实践》 全套PPT课件
c语言·c#·c语言程序设计
叶帆1 天前
【YFIOs】用C#开发硬件之设备上云
开发语言·unity·c#
IT方大同1 天前
(嵌入式操作系统)信号量
嵌入式硬件·c#
c++之路1 天前
备忘录模式(Memento Pattern)
c++·microsoft
Solis程序员1 天前
MCP (Model Context Protocol):AI应用连接外部世界的标准协议
人工智能·microsoft·agent·skill·mcp