.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传值,只不过一个是存在服务器,一个是客户端浏览器中。

你可能感兴趣的:(.net,core,前端,.netcore,Blazor)