How To Pass Data From One Blazor Page To Another

Hello whether you are using Radzen or Visual Studio version of Blazor, you have three options

  1. Pass the value as a parameter. Look this up. But this is not secure because users can see the value you passed.
  2. Used Blazored.Session storage package from nuget. This also is not secure
  3. 3.Use ProtectedSessionStorage from Microsoft.AspNetCore.Components.Server.ProtectedBrowserStorage package. This third option is secure. See code snippet below:-

//first page

using Microsoft.AspNetCore.Components.Server.ProtectedBrowserStorage;

public partial class FirstPageComponent

{

[Inject]


    ProtectedSessionStorage sessionStorage { get; set; }        

//code to pass value

private async Task SetOrderID()

{

int orderID=10;

await sessionStorage.SetAsync("NewOrderID", orderID);

UriHelper.NavigateTo("SecondPage");//code to redirect to second page

}

}

//second page

using Microsoft.AspNetCore.Components.Server.ProtectedBrowserStorage;

public partial class SecondPageComponent

{

[Inject]


    ProtectedSessionStorage sessionStorage { get; set; }        

//code to retrieve value

private async Task GetOrderID()

{

ProtectedBrowserStorageResult result = await sessionStorage.GetAsync("NewOrderID");


            if (result.Success)
            {
                .........................................
            }        

}

}

要查看或添加评论,请登录

社区洞察

其他会员也浏览了