dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.36k stars 9.99k forks source link

[Blazor] UriHelper.NavigateTo with Parameters from a layout, requires the current page to have the same parameter (with repro step) #10729

Closed RemiBou closed 5 years ago

RemiBou commented 5 years ago

Describe the bug

This issue contains repro steps for #7023 When you invoke NavigateTo from a layout, it tries to parse the target url pattern with the current page. So this kind of exception is raised " Object of type 'Toss.Client.Pages.Home.Index' does not have a property matching the name 'tag'. ". But the page is loaded correctly.

To Reproduce

If you have any trouble with the repro steps please ask

Expected behavior

The navigation occurs without the error.

RemiBou commented 5 years ago

@danroth27

Here are easier step for repro

git clone https://github.com/RemiBou/Toss.Blazor.git
cd Toss.Blazor
git checkout origin/pure-css-no-bootstrap
cd Toss.Tests.E2E
dotnet test

You will see the error message displayed on the web page during the e2e tests

danroth27 commented 5 years ago

OK, I see the error:

image

danroth27 commented 5 years ago

Here's the relevant code from the layout:

https://github.com/RemiBou/Toss.Blazor/blob/pure-css-no-bootstrap/Toss.Client/Shared/Layouts/MainLayout.razor

@inject IExceptionNotificationService exceptionNotificationService;
@inject IAccountService accountService;
@inject IHttpApiClientRequestBuilderFactory ClientFactory;
@inject IUriHelper urlHelper;
@implements IDisposable;
@inject IMessageService messageService;
<div class="pure-g">
    <div class="top-menu pure-menu pure-u-5-5 pure-menu-horizontal pure-menu-scrollable">
        <a href="#" class="pure-menu-link pure-menu-heading">Toss</a>
        <ul class="pure-menu-list">
            @if (_account != null)
            {
                <li class="pure-menu-item">
                    <NavLink href="/account" class="pure-menu-link" id="LinkAccount">
                        <Icon Title="Account" IconType="person"></Icon>
                    </NavLink>
                </li>

            }
            else
            {
                <li class="pure-menu-item">
                    <NavLink href="/login" class="pure-menu-link" id="LinkLogin">
                        Login
                    </NavLink>
                </li>
            }
            <li class="pure-menu-item">
                <NavLink href="/new" class="pure-menu-link" id="LinkNewToss">
                    <Icon Title="New Toss" IconType="edit"></Icon>
                </NavLink>
            </li>
            @if (_account != null)
            {
                @foreach (var hashTag in _account.Hashtags ?? new List<string>())
                {
                    <li class="pure-menu-item">
                        <NavLink href=@("/tag/"+hashTag) Match="NavLinkMatch.All" class="pure-menu-link  tag-link">
                            #@hashTag
                        </NavLink>
                    </li>
                }
            }
            <li class="pure-menu-item">
                <div class="pure-form">
                    <EditForm OnValidSubmit="SeeHashtag" Model="@addHashtagCommand">
                        <MyInput Placeholder="Tag" Type="text" Id="TxtAddHashTag" bind-Value="@addHashtagCommand.NewHashTag" />
                    </EditForm>
                </div>
            </li>
        </ul>
    </div>
</div>
<Message />
<div class="main">
    @Body
</div>
@functions {

    [Parameter]
    RenderFragment Body { get; set; }

    AddHashtagCommand addHashtagCommand = new AddHashtagCommand();

    private AccountViewModel _account;
    protected override void OnInit()
    {
        this.exceptionNotificationService.OnException += HandleExceptions;
        this.accountService.SubscribeOnCurrentAccountChanged(this.OnAccountChanged);
        base.OnInit();
    }

    private void OnAccountChanged(object sender, AccountViewModel account)
    {
        this._account = account;
        this.StateHasChanged();
    }
    private void HandleExceptions(object sender, string s)
    {
        messageService.Error(s);
    }
    public void Dispose()
    {

        this.exceptionNotificationService.OnException -= HandleExceptions;
    }
    protected void SeeHashtag()
    {
        urlHelper.NavigateTo("/tag/" + addHashtagCommand.NewHashTag);
    }
}
RemiBou commented 5 years ago

@danroth27 I think the error is gone with preview6, did you change anything between 5 and 6 that could have impacted this ?

danroth27 commented 5 years ago

@RemiBou It's certainly sounds like we did :smile:. @SteveSandersonMS?

SteveSandersonMS commented 5 years ago

I don't know of anything specific we changed for this. Maybe the exact details of your repro scenario rely on something more obscure I can't anticipate.

mkArtakMSFT commented 5 years ago

Closing as looks like this is fixed.