我创建了一个Blazor Web App项目,设置如下.

enter image description here

当我启动时,尽管Counter.razor组件包含@rendermode InteractiveAuto,但计数器页面并不下载WASM并从服务器切换.在浏览器的网络选项卡中,WebSocket保持活动状态.

此外,我将以下代码放入NavMenu.razor组件中.文本永远不会切换到WASM.

<h1 class="text-muted">Render @(OperatingSystem.IsBrowser() ? "Wasm" : "Server")</h1>

根据this video,它应该下载WASM并进行切换.

为了解决这个问题,我try 使用不同的浏览器,并使用Interactive Auto Render模式制作自己的组件.

这里的问题是什么?

客户端项目中的Counter.razor个文件:

@page "/counter"
@rendermode InteractiveAuto

<PageTitle>Counter</PageTitle>

<h1>Counter</h1>

<p role="status">Current count: @currentCount</p>

<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>

@code {
    private int currentCount = 0;

    private void IncrementCount()
    {
        currentCount++;
    }
}

客户端项目中的Program.cs个文件:

using Microsoft.AspNetCore.Components.WebAssembly.Hosting;

var builder = WebAssemblyHostBuilder.CreateDefault(args);

await builder.Build().RunAsync();

服务器项目中的Program.cs个文件:

using TestBlazorProj.Client.Pages;
using TestBlazorProj.Components;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddRazorComponents()
    .AddInteractiveServerComponents()
    .AddInteractiveWebAssemblyComponents();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseWebAssemblyDebugging();
}
else
{
    app.UseExceptionHandler("/Error", createScopeForErrors: true);
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    app.UseHsts();
}

app.UseHttpsRedirection();

app.UseStaticFiles();
app.UseAntiforgery();

app.MapRazorComponents<App>()
    .AddInteractiveServerRenderMode()
    .AddInteractiveWebAssemblyRenderMode()
    .AddAdditionalAssemblies(typeof(Counter).Assembly);

app.Run();

App.razor文件在服务器项目中.

 <!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <base href="/" />
    <link rel="stylesheet" href="bootstrap/bootstrap.min.css" />
    <link rel="stylesheet" href="app.css" />
    <link rel="stylesheet" href="TestBlazorProj.styles.css" />
    <link rel="icon" type="image/png" href="favicon.png" />
    <HeadOutlet />
</head>

<body>
    <Routes />
    <script src="_framework/blazor.web.js"></script>
</body>

</html>

推荐答案

因为您的NavMenu组件仍然是静态的(默认情况下),将渲染模式应用于每页@rendermode InteractiveAuto将仅影响当前组件;它不会影响父组件中的其他子组件.

如果您添加此行

<h1 class="text-muted">Render @(OperatingSystem.IsBrowser() ? "Wasm" : "Server")</h1>

在您的计数器组件中,您将看到:

enter image description here

您可以查看此文档以了解更多详细信息:Child components with different render modes

Csharp相关问答推荐

我应该将新的httpReportMessage()包装在using声明中吗?

PredicateBuilder不是循环工作,而是手动工作

如何将字节数组转换为字符串并返回?

Unity 2D自顶向下弓旋转

与C#中的Zip列表并行

Take()方法如何与IAsyncEnumerable一起使用

在具有不同属性名称的两个类之间创建关系

C#带主体的主构造函数?

未在数据流块之间传播完成

如何将端点(或с匹配请求并判断其路径)添加到BCL?

Regex字母数字校验

将操作从编辑页重定向到带参数的索引页

为什么Docker中没有转发该端口?

将FileStream的特定部分作为字节数组读取

MSI无法将快捷方式添加到启动文件夹

Blazor服务器项目中的Blazor/.NET 8/Web API不工作

使用C#12中的主构造函数进行空判断

SqlException:无法打开数据库.升级到Dotnet 8后-数据库兼容性版本-非EFCore兼容性级别

在使用xUnit和Mock执行单元测试时,控制器ViewResult返回空的Model集合

外部应用&&的LINQ;左外部连接&类似于PostgreSQL的查询