NavigationManager.NavigateTo - Blazor

etienne_marais

Honorary Master
Joined
Mar 16, 2008
Messages
15,905
Reaction score
19,062
Location
Centurion
I am following a udemy course and have done everything the same as the course.

I have an EditServer.razor page

Code:
@page "/servers/{id:int}"

@inject NavigationManager NM

<h3>Edit Server</h3>

@if (server != null)
{
    <EditForm Model="server" FormName="formServer" OnValidSubmit="Submit">
        <DataAnnotationsValidator></DataAnnotationsValidator>
        <ValidationSummary></ValidationSummary>

        <InputNumber @bind-Value=server.Id hidden></InputNumber>
        <div class="row mb-3">
            <div class="col-2">
                <label class="col-form-label">Name</label>
            </div>
            <div class="col-6">
                <InputText @bind-Value="server.Name" class="form-control"></InputText>
            </div>
            <div class="col">
                <ValidationMessage For="() => server.Name"></ValidationMessage>
            </div>
        </div>

        <div class="row mb-3">
            <div class="col-2">
                <label class="col-form-label">City</label>
            </div>
            <div class="col-6">
                <InputText @bind-Value="server.City" class="form-control"></InputText>
            </div>
            <div class="col">
                <ValidationMessage For="() => server.City"></ValidationMessage>
            </div>
        </div>

        <div class="row-mb3">
            <div class="col-2">
                <label class="col-form-label">Online</label>
            </div>
            <div class="col-6">
                <InputCheckbox @bind-Value="server.IsOnline" class="form-check-input"></InputCheckbox>
            </div>
        </div>
        <br />
        <button class="btn btn-primary" type="submit">Update</button>
        &nbsp;
        <a href="/servers" class="btn btn-primary">Close</a>
    </EditForm>
}

@code {
    [Parameter]
    public int Id { get; set; }

    [SupplyParameterFromForm]
    private Server? server { get; set; }

    protected override void OnParametersSet()
    {
        server ??= ServersRepository.GetServerById(this.Id);
    }

    private void Submit()
    {
        if (server != null)
        {
            ServersRepository.UpdateServer(server.Id, server);
        }

        try
        {
            NM.NavigateTo("/");
        } catch (Exception ex) {
            ;
        }
    }
}

Everything works, but NavigateTo throws an exception:



I have tried various valid NavigateTo endpoints but it always throws an exception.

Any ideas ?
 
Everything works, but NavigateTo throws an exception:
The Exception:

Code:
?ex
{"Exception of type 'Microsoft.AspNetCore.Components.NavigationException' was thrown."}
    Data: {System.Collections.ListDictionaryInternal}
    HResult: -2146233088
    HelpLink: null
    InnerException: null
    Location: "https://localhost:7057/servers"
    Message: "Exception of type 'Microsoft.AspNetCore.Components.NavigationException' was thrown."
    Source: "Microsoft.AspNetCore.Components.Endpoints"
    StackTrace: "   at Microsoft.AspNetCore.Components.Endpoints.HttpNavigationManager.NavigateToCore(String uri, NavigationOptions options)\r\n   at Microsoft.AspNetCore.Components.NavigationManager.NavigateToCore(String uri, Boolean forceLoad)\r\n   at Microsoft.AspNetCore.Components.NavigationManager.NavigateTo(String uri, Boolean forceLoad, Boolean replace)\r\n   at ServerManagement.Components.Pages.EditServer.Submit() in C:\\Users\\etien\\source\\repos\\Learn\\BLAZOR\\BlazorDeepDive\\ServerManagement\\Components\\Pages\\EditServer.razor:line 74"
    TargetSite: {Void NavigateToCore(System.String, Microsoft.AspNetCore.Components.NavigationOptions)}
?ex.InnerException
null
 
Top
Sign up to the MyBroadband newsletter