繁体   English   中英

Blazor 服务器-一台 URL

[英]Blazor Server - one URL

我需要向页面传递一个参数:

www.website/{id}/{anotherID}

www.website.com/21/321

我不想让用户看到参数:

www.website.comwww.website.com/books

请问在blazor中是否可行? 我真的不需要将它作为路由参数传递,但不知道任何其他方法。

有几种方法。 这是 DI 服务方式。

创建一个 class 来保存你的数据:

public class MyPageData
{
    public int Value { get; set; }
}

在 DI 服务中注册

builder.Services.AddServerSideBlazor();
builder.Services.AddScoped<MyPageData>();

使用示例:

@page "/"
@inject MyPageData myPageData
@inject NavigationManager NavManager

<PageTitle>Index</PageTitle>

<h1>Hello, world!</h1>

Welcome to your new app.

<SurveyPrompt Title="How is Blazor working for you?" />

<button class="btn btn-primary" @onclick=this.GottoCounter>Go to Counter Page</button> 

@code {
    private void GottoCounter()
    {
        this.myPageData.Value = 10;
        NavManager.NavigateTo("/Counter");
    }
}

和柜台:

@page "/counter"
@inject MyPageData myPageData

<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;

    protected override void OnInitialized()
    {
        currentCount = myPageData.Value;
    }

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM