Blazor 组件未执行 NavManager.NavigateTo

如何解决Blazor 组件未执行 NavManager.NavigateTo

我有一个奇怪的现象,我有一个登录屏幕,它需要输入两个文本框的用户名和密码,它会检查用户是否有帐户并带回用户 ID..

enter image description here

但是当我通过单击按钮调用方法时,由于某种奇怪的原因页面刷新并且此查询字符串弹出我的栏

enter image description here

然后代码开始执行...它也不会导航我请求的页面...

我买回的数据很好,但我显然不了解生命周期的工作原理..有什么建议吗?

我的组件

<html>
<body>
    <div class="container">
        <div class="header-text">
            &nbsp;&nbsp;&nbsp;<img style="height:200px; width:200px" class="header-text-logo" src="/logo/WebbiSkoolslogo.jpg" alt="logo">
            <p class="header-text-description">Student Login</p>
        </div>
        <form>
            <input type="text"
                   placeholder="Username"
                   name="username"
                   id="username"
                   @oninput="@((e) => { Username = (string)e.Value; })"
                   autofocus>
            <input type="password"
                   name="password"
                   id="password"
                   @oninput="@((e) => { Password = (string)e.Value; })"
                   placeholder="Password"
                   required>
            <button @onclick="ValidateUser" style="cursor:pointer" type="submit" name="login">Login</button>
            <button @onclick="AddUserTest" style="cursor:pointer" type="submit" name="login">Add User</button>
            <div class="text-lg-center">
                @*<p style="color:red"><h1>@Errors</h1></p>*@
            </div>
        </form>
    </div>
</body>
</html>

组件代码

@code {
    private string Username { get; set; }
    private string Password { get; set; }
    private string Errors = string.Empty;
    private int UserId = 0;
    public bool IsShow { get; set; } = false;

    private void ValidateUser()
    {

        string user = Username;
        string pass = Password;

        UserId =  TryLogin(user,pass);

        if (UserId != 0)
        {
            NavManager.Navigateto("QuizEdit");
        }
        else
        {
            Errors = "Failed to log in";
        }

    }

背后的代码

public int TryLogin(string user,string pass)
        {
            connectionString = Settings.Value.ConnectionString;
            Login login = new Login();
            UserId =  login.GetUserIdByUsernameAndPassword(user,pass,connectionString);
            return UserId;
        }

还有我的登录检查

public int GetUserIdByUsernameAndPassword(string username,string password,string connectionString)
        {
            // this is the value we will return
            int userId = 0;
            
            
            sqlConnection con = new sqlConnection(connectionString);
            using (sqlCommand cmd = new sqlCommand("SELECT UserId,Password,Guid FROM [UserDetails] WHERE username=@username",con))
            {
                cmd.Parameters.AddWithValue("@username",username);
                con.open();
                sqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    // dr.Read() = we found user(s) with matching username!

                    int dbUserId = Convert.ToInt32(dr["UserId"]);
                    string dbPassword = Convert.ToString(dr["Password"]);
                    string dbUserGuid = Convert.ToString(dr["Guid"]);

                    // Now we hash the UserGuid from the database with the password we wan't to check
                    // In the same way as when we saved it to the database in the first place. (see AddUser() function)
                    string hashedPassword = Security.HashSHA1(password + dbUserGuid);

                    // if its correct password the result of the hash is the same as in the database
                    if (dbPassword == hashedPassword)
                    {
                        // The password is correct
                        userId = dbUserId;
                    }
                }
                con.Close();
            }

            // Return the user id which is 0 if we did not found a user.
            return userId;
        }

解决方法

问题在于您在表单上使用“提交”按钮。当您单击它们中的每一个时,就会发生 HTTP post 请求。但这不是你的本意。您想调用 ValidateUser 和 AddUserTest,但是您向服务器发送了一个毫无意义的发送请求。您应该改为使用带有 type="button" 或 button 元素的 input 元素

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?