.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时候有什么不同的。

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

相关推荐
PfCoder4 小时前
C#中定时器之System.Timers.Timer
c#·.net·visual studio·winform
csdn_aspnet8 小时前
ASP.NET Core 中的依赖注入
后端·asp.net·di·.net core
ahxdyz9 小时前
.NET平台MCP
ai·.net·mcp
の天命喵星人9 小时前
.net 使用NLog记录日志
.net
绿荫阿广10 小时前
将SignalR移植到Esp32—让小智设备无缝连接.NET功能拓展MCP服务
.net·asp.net core·mcp
人工智能AI技术10 小时前
【C#程序员入门AI】本地大模型落地:用Ollama+C#在本地运行Llama 3/Phi-3,无需云端
人工智能·c#
csdn_aspnet14 小时前
ASP.NET 8 - Cookie 身份验证
后端·asp.net·cookie·.net8
MyBFuture14 小时前
C#数组详解:一维二维与交错数组
开发语言·windows·c#·visual studio·vision pro
TOOLS指南15 小时前
谷歌AI Gemin怎么使用?Gemini国内使用指南!
数据库·微软