Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 并非所有代码路径都在我的控制器方法中返回值_C#_Asp.net_Asp.net Mvc - Fatal编程技术网

C# 并非所有代码路径都在我的控制器方法中返回值

C# 并非所有代码路径都在我的控制器方法中返回值,c#,asp.net,asp.net-mvc,C#,Asp.net,Asp.net Mvc,我目前正在做一个登录页面,根据他们的员工角色指向任何一个页面。 例如,如果StaffRole=Manager,则直接转到Manager页面。下面是我的控制器方法代码。但是,我的控制器方法显示了一个错误,该错误表示并非所有代码路径都返回一个值。我不知道如何解决这个问题 ` [HttpPost] public ActionResult Verify(Account acc) { connectionString(); con.Open();

我目前正在做一个登录页面,根据他们的员工角色指向任何一个页面。 例如,如果StaffRole=Manager,则直接转到Manager页面。下面是我的控制器方法代码。但是,我的控制器方法显示了一个错误,该错误表示并非所有代码路径都返回一个值。我不知道如何解决这个问题

` [HttpPost]
    public ActionResult Verify(Account acc)
    {
        connectionString();
        con.Open();
        com.Connection = con;
        com.CommandText = "select * from Staff where StaffNRIC='" + acc.StaffNRIC + "' and StaffContact='" + acc.StaffContact + "' and StaffAccountStatus = 'Approved'";
        dr = com.ExecuteReader();
        if (dr.Read())
        {
            if (dr.HasRows)
            {
                while (dr.Read())
                {
                    if (dr["StaffRole"].ToString() == "Manager")
                    {
                        dr.Close();
                        return RedirectToAction("Manager/ManagerHome", "ManagerController");//wherever you want to return the user to 

                    }
                    else if (dr["StaffRole"].ToString() == "Admin")
                    {
                        dr.Close();
                        return RedirectToAction("Admin/AdminHome", "AdminController");
                    }
                    else if (dr["StaffRole"].ToString() == "Part-Timer")
                    {
                        dr.Close();
                        return RedirectToAction("PartTimer/PartTimerHome", "PartTimerController");
                    }
                    else
                    {
                        con.Close();
                        return View("Login");
                    }
                }
            }
        }
    }`

'
SqlConnection con=newsqlconnection();SqlCommand com=newsqlcommand();SqlDataReader-dr

正如错误消息所示,并非代码中的所有路径都返回值。例如,如果
dr.Read()。要解决此错误,只需添加
返回视图(“登录”)
if(dr.Read())
块之后

[HttpPost]
public ActionResult Verify(Account acc)
{
    connectionString();
    con.Open();
    com.Connection = con;
    com.CommandText = "select * from Staff where StaffNRIC='" + acc.StaffNRIC + "' and StaffContact='" + acc.StaffContact + "' and StaffAccountStatus = 'Approved'";
    dr = com.ExecuteReader();
    if (dr.Read())
    {
        if (dr.HasRows)
        {
            while (dr.Read())
            {
                if (dr["StaffRole"].ToString() == "Manager")
                {
                    dr.Close();
                    return RedirectToAction("Manager/ManagerHome", "ManagerController");//wherever you want to return the user to 

                }
                else if (dr["StaffRole"].ToString() == "Admin")
                {
                    dr.Close();
                    return RedirectToAction("Admin/AdminHome", "AdminController");
                }
                else if (dr["StaffRole"].ToString() == "Part-Timer")
                {
                    dr.Close();
                    return RedirectToAction("PartTimer/PartTimerHome", "PartTimerController");
                }
                else
                {
                    con.Close();
                    return View("Login");
                }
            }
        }
    }
    return View("Login");
}

就像@Loong的回答一样,您的控制器可能并不总是返回某些内容,IMO最好的方式是在if/else等中设置一个对象。然后您返回可以初始化到登录页面的对象。因此,我添加了“output”,这就是我返回的内容,当我可以的时候,将其设置为登录页面以外的其他内容。您还可以使用查找多次从数据中读取相同的信息(请参阅我的替换开关)

还可以考虑使用usings进行重构,以便自动处理连接,如下所示,我假设您使用的是SQLConnection和SQLCommand,但是这种方法应该适用于任何iDisposable对象

另外,在使用sql命令文本时,可以使用诸如@accName之类的sql变量来限制调用此函数和使用sql注入的可能性(我在这里没有这样做)

[HttpPost]
public ActionResult Verify(Account acc)
{
    ActionResult output = View("Login");

    //Usings auto close and dispose of objects once you break out of them
    using(var conn = GetNewConnection()) {
        using(var cmd = new SQLCommand("select * from Staff where StaffNRIC='" + acc.StaffNRIC + "' and StaffContact='" + acc.StaffContact + "' and StaffAccountStatus = 'Approved'", conn)) {
            dr = com.ExecuteReader();

            if(dr.Read()) {
                bool isFound = false;

                if(dr.HasRows()) {
                    while(dr.read()) {
                       var role = dr["StaffRole"].ToString();

                       switch(role) {
                          case: "Manager": output = RedirectToAction("Manager/ManagerHome", "ManagerController"); isFound=true; break;
                          case: "Admin": output = RedirectToAction("Admin/AdminHome", "AdminController"); isFound=true; break;
                          case: "StaffRole": output = RedirectToAction("PartTimer/PartTimerHome", "PartTimerController"); isFound = true; break;
                       }

                       if(isFound) { //This bit breaks out of the while->read of the db if the role has been found.
                           break;
                       }
                    }
                }
            }
        }

    return output;
    }