.net core 通过ProtectedSessionStorage 会话存储进行页面传值。案例前端采用Blazor

应用场景,再跳转后不打开多个Tab的情况下,假如你需要从一个页面传一个固定参数跳转到另一个固定页面,进行数据筛选,就可以使用一下方式进行。

一、在要操作的页面进行如下声明:
1.先引入对应的命名空间和一些相关的注入。

@using Microsoft.AspNetCore.Components.Server.ProtectedBrowserStorage;
@inject ProtectedSessionStorage ProtectedSessionStore

2.假如当前页面只有一个按钮。用来进行点击跳转到其他页面。

    <Button OnClick="@( async()=>{ await navToItem(SampleStatus.Default); })" style="width: 355px; z-index:333; height: 166px; background-color: #5470C6; position: relative" Color="Color.Light">

                <p style="font-size: 12px; color: white;right:5px; top:15px; z-index: 999; position:absolute">点击查看更多</p>
     

        </Button>

3.对ProtectedSessionStore进行相关配置

  //声明一个key
  private static string navKey = Guid.NewGuid().ToString("N");
  //设置传入的参数,并绑定给key
    private async Task navToItem(SampleStatus value)
    {   //配置ProtectedSessionStore
        await ProtectedSessionStore.SetAsync(navKey, value.ToString());
        //跳转的页面
        NavigationManager.NavigateTo($"_Sample/SlecteSample/{navKey}", WtmBlazor.Localizer["Text"]);
        await Task.CompletedTask;
    }

二、接收的页面,做如下操作
1.做相关声明

@page "/_Sample/SlecteSample/{navKey?}"
@using Microsoft.AspNetCore.Components.Server.ProtectedBrowserStorage;
@inject ProtectedSessionStorage ProtectedSessionStore;


 [Parameter]
    public string? navKey { get; set; }


    [Parameter]
    public string? Status { get; set; }

2.通过ProtectedSessionStore.GetAsync进行接收

 var res = await ProtectedSessionStore.GetAsync<string>(navKey);
 Status = res.Value;

该方式,有点像Session传值,只不过一个是存在服务器,一个是客户端浏览器中。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值