使用 Blazor Server 检查后提交表单时如何重定向到另一个页面?

How can I redirect to another page when a form is submitted after being checked using Blazor Server?

我目前正在使用 Blazor Server 开发登录和注册页面。在我的表单中提交字段后,是否可以重定向到另一个 .razor 页面?我正在调用一个 submitFields 函数来通过这个特定示例的“@onclick”事件处理程序来验证登录信息。在 submitFields 函数中,我尝试使用 Navigation Manager,但它什么也没做。此外,我尝试重新初始化页面并将检查设置为 true,这样我就可以重定向,但我也遇到了这种方法的问题。有任何想法吗?下面是我的源代码。

@code {
    private string email;
    private string password;
    private bool check = false;
    private string mySetting = "";

    // form input logic
    private void submitFields()
    {
        User person = new User();
        person.connect = mySetting;
        person.email = email;
        person.password = password;
        //when decrypting use Encrypt.hashString(password)
        person.logIn();
        check = person.check;
        // I want to redirect to another page here
        /*
        if(check == true)
        {
            yay youre logged in, lets go to a new page!
        } else
        {
            dont go anywhere
        }
        */
    }

    // on load -> call this func
    private void initMethod()
    {
        mySetting = _config.GetValue<string>("MySetting"); //on load get hidden connection string from appsettings.json
    }

    // on load
    protected override async Task OnInitializedAsync()
    {
        initMethod();
    }
}

首先你必须注入 NavigationManager。然后您可以使用它导航到另一个页面。示例:

@inject NavigationManager NavigationManager

@code {
    private void submitFields()
    {
        User person = new User();
        person.connect = mySetting;
        person.email = email;
        person.password = password;
        //when decrypting use Encrypt.hashString(password)
        person.logIn();
        check = person.check;
        // I want to redirect to another page here

        if(check == true)
        {

            NavigationManager.NavigateTo("/account/dashboard");

        } else
        {
            dont go anywhere
        }

    }
}