粘贴链接时参数为空异常

我有一个Blazor Web组装应用程序,该应用程序具有一个主页和一个嵌套组件:

@page "/work/{CustomerId:guid}"

@using System.Diagnostics
@using ApplySupportTool.Client.I18nText
@using ApplySupportTool.Client.Services.Contracts
@using ApplySupportTool.Shared.Dto.Customer
@using Toolbelt.Blazor.I18nText

@inject I18nText Localization
@inject ICustomerApi CustomerApi

@attribute [Authorize]


<h3>@string.Format(localizedStrings.WorkOverviewTitle,Customer?.CustomerName)</h3>

<MatTabGroup>
    <MatTab Label="@localizedStrings.TimeTrackingLabel">
        <CascadingValue Value="@CustomerId">
            <TimeTrackingList />
        </CascadingValue>
    </MatTab>

    <MatTab Label="@localizedStrings.WorkItemsLabel">
        Work Items
    </MatTab>
</MatTabGroup>

@code {

    [Parameter]
    public Guid CustomerId { get; set; }

    public CustomerDto Customer { get; set; }
    LocalizedStrings localizedStrings = new LocalizedStrings();

    /// <inheritdoc />
    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();
        localizedStrings = await Localization.GetTextTableAsync<LocalizedStrings>(this);
        Customer = await CustomerApi.getcustomerByIdAsync(CustomerId);
    }
}

以及嵌套的组件:

@using ApplySupportTool.Client.I18nText
@using ApplySupportTool.Client.Components.Modals
@using ApplySupportTool.Client.Services.Contracts
@using Toolbelt.Blazor.I18nText

@inject I18nText Localization
@inject IModalService Modal
@inject ITimeTrackingApi TimeTrackingApi

@if (false)
{
    <LoadingDialog Title="@localizedStrings.LoadingLabel" IsOpen="true" />
} else
{
    <br />
    <MatButton Label="@localizedStrings.NewTimeTrackingLabel" Raised="true" OnClick="@OpenCreateTimeTrackingDialog" />

    <br />
    <br />

    ... 
    // Mat Blazor accordion
}

@code {

    [CascadingParameter]
    public Guid CustomerId { get; set; }

    LocalizedStrings localizedStrings = new LocalizedStrings();

    public List<TimeTrackingDto> TimeTrackingDtoList = new List<TimeTrackingDto>();

    /// <inheritdoc />
    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();
        localizedStrings = await Localization.GetTextTableAsync<LocalizedStrings>(this);

        TimeTrackingDtoList = await TimeTrackingApi.GetTimeTrackingAsync(CustomerId);
    }   
}

当我在浏览器中导航时,这可以按预期工作。但是,当我复制链接时,它会生成并再次粘贴以加载页面,这会导致很多wasm错误:

粘贴链接时参数为空异常

我检查了我具有string.format的页面,但找不到任何问题。因此,我认为它可能是在解析参数之类的东西。

有什么解决方法吗?

EDIT1: 如评论中所建议,我检查了Guid是否正确传递。在常规导航中,它随处可见。但是,当我粘贴链接并以这种方式导航时,我只能在主页上找到它,但是似乎它没有传递给嵌套组件。 我也尝试用默认的字符串参数更改替换Guid类型。但这并没有改变任何东西。都没有用普通参数替换级联参数。

qizheyu2009 回答:粘贴链接时参数为空异常

子组件中的

[Parameter]
    public Guid CustomerId { get; set; }

应该是

[CascadingParameter]
    public Guid CustomerId { get; set; }
,

我有以下代码将显示一个对话框:

<MatDialog @bind-IsOpen="@deleteCustomerDialogOpen" Style="z-index: 100">
    <MatDialogTitle>
        <MatIcon Icon="warning" />@localizedStrings.ConfirmDeleteTitle
    </MatDialogTitle>
    <MatDialogContent>
        @string.Format(localizedStrings.DeleteCustomerConfirmationMessage,SelectedCustomerDto?.CustomerName)
    </MatDialogContent>
    <MatDialogActions>
        <MatButton OnClick="@DeleteCustomerAsync">@localizedStrings.DeleteLabel</MatButton>
        <MatButton OnClick="@(e => { deleteCustomerDialogOpen = false; })">@localizedStrings.CancelLabel</MatButton>
    </MatDialogActions>
</MatDialog>

并且本地化初始化如下:     LocalizedStrings localizedStrings = new LocalizedStrings();

/// <inheritdoc />
protected override async Task OnInitializedAsync() {
    localizedStrings = await Localization.GetTextTableAsync<LocalizedStrings>(this);        
}

问题似乎是,在重新加载string.format之前,未正确加载localizedStrings。因此,这引起了问题。我将代码更改为以下代码进行了修复:

@string.Format(localizedStrings.DeleteCustomerConfirmationMessage,SelectedCustomerDto?.CustomerName)
本文链接:https://www.f2er.com/3055196.html

大家都在问