You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Blazor多页面流程中如何跨页传递用户提交的大量信息?

三页面业务流程

Blazor 多页面跨页传递大量数据实现方案

路由参数仅适合传递短文本类简单参数,传递长文本、结构化内容可选择以下3种方案:

1. 作用域状态服务(最适配业务流程场景)

通过注入自定义的作用域服务存储临时业务数据,流程内所有页面均可读写:

  1. 定义数据承载模型
public class BusinessFlowData
{
    // 可自定义所有需要传递的字段
    public string PublicNoticeContent { get; set; }
    public string PersonnelInfo { get; set; }
}
  1. 在Program.cs中注册服务
// 作用域服务在单次用户会话内有效,不同用户数据隔离
builder.Services.AddScoped<BusinessFlowData>();
  1. 页面读写数据
    每个页面顶部注入服务:
@inject BusinessFlowData FlowData

页面1提交数据时赋值:

FlowData.PublicNoticeContent = "Treasury Secretary Janet Yellen is testifying with Fed Chairman Jerome Powell before lawmakers after Senate Republicans blocked a bill that would both fund the government and lift the borrowing limit.";
FlowData.PersonnelInfo = "Mr. John Doe\n\n08:30 09/30/2021 Washington";
// 跳转页面2

页面2、页面3直接读取FlowData的对应属性即可,流程结束后可主动清空属性避免数据残留。

2. 浏览器会话存储(适合刷新不丢数据的场景)

通过sessionStorage存储序列化后的JSON数据,标签页关闭后自动清理:
页面顶部注入JS运行时:

@inject IJSRuntime JSRuntime

页面1存储数据:

// 序列化数据为JSON字符串
var data = new 
{
    Content = "长文本内容",
    Info = "用户信息"
};
string json = System.Text.Json.JsonSerializer.Serialize(data);
await JSRuntime.InvokeVoidAsync("sessionStorage.setItem", "flowTempData", json);

其他页面读取数据:

string json = await JSRuntime.InvokeAsync<string>("sessionStorage.getItem", "flowTempData");
if (!string.IsNullOrEmpty(json))
{
    var flowData = System.Text.Json.JsonSerializer.Deserialize<BusinessFlowData>(json);
    // 业务逻辑处理
}

3. 级联参数(适合同父布局的嵌套页面)

如果三个页面均挂载在同一个父布局/父组件下,可在父组件通过CascadingValue传递数据,子页面用CascadingParameter接收即可,无需额外传参操作。

注意事项

  • 每个页面加载时建议做数据合法性校验,数据为空时可跳回流程起始页,避免用户直接访问后续流程页面出错。
  • 敏感数据不要存储在localStorage中,优先选择作用域服务或sessionStorage。

内容的提问来源于stack exchange,提问作者Đỗ Như Vỹ

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.02 10:36:03