如何在ASP.NET MVC中登录时重定向到不同的页面?

时间:2022-05-27 17:06:55

I created a registration page where there is a drop down menu that allows to user to select a user type (Tutor or Student).

我创建了一个注册页面,其中有一个下拉菜单,允许用户选择用户类型(教师或学生)。

They'll be redirected to Login page and they should be redirected to different pages depending on the type.

它们将被重定向到“登录”页面,应根据类型将它们重定向到不同的页面。

AccountController.cs

[HttpPost] //"Login" Button
public ActionResult Login(UsersModel record)
{
    using (SqlConnection con = new SqlConnection(Helper.GetConnection()))
    {
        con.Open();
        string query = @"SELECT UserID, TypeID FROM Users WHERE Email=@Email AND Password=@Password AND Status!=@Status";


        using (SqlCommand cmd = new SqlCommand(query, con))
        {
            cmd.Parameters.AddWithValue("@Email", record.Email);
            cmd.Parameters.AddWithValue("@Password", record.Password);
            cmd.Parameters.AddWithValue("@Status", "Suspended");

            using (SqlDataReader data = cmd.ExecuteReader())
            {
                if (data.HasRows)
                {

                    while (data.Read())
                    {
                        Session["userid"] = data["UserID"].ToString();
                        Session["typeid"] = data["TypeID"].ToString();
                    }

                    if ((string)Session["typeid"] == "Student")

                        return RedirectToAction("Profile");

                    if ((string)Session["typeid"] == "Tutor")

                        return RedirectToAction("TutorProfile");
                }
                else
                {
                    ViewBag.Error = "<div class = alert alert-danger col-lg-6'>Invalid Credentials.</div>";
                    return View();
                }
            }
        }
    }
}

In Visual Studio, I am getting the error:

在Visual Studio中,我收到错误:

"not all code paths return value"

“并非所有代码路径都返回值”

Doesn't the return RedirectToAction count as return value?

返回RedirectToAction不会计为返回值吗?

4 个解决方案

#1


0  

What if typeid is neither Student or Tutor?

如果typeid既不是学生也不是导师怎么办?

Consider either:

if ((string)Session["typeid"] == "Student")
    return RedirectToAction("Profile");
else if ((string)Session["typeid"] == "Tutor")
    return RedirectToAction("TutorProfile");

or:

if ((string)Session["typeid"] == "Student")
    return RedirectToAction("Profile");
if ((string)Session["typeid"] == "Tutor")
    return RedirectToAction("TutorProfile");
ViewBag.Error = "<div class = alert alert-danger col-lg-6'>Invalid User Type.</div>";
return View();

#2


0  

Look the changes below.

查看下面的更改。

string sRedirecttoAction = String.Empty;

string sRedirecttoAction = String.Empty;

using (SqlConnection con = new SqlConnection(Helper.GetConnection()))
{
    con.Open();
    string query = @"SELECT UserID, TypeID FROM Users WHERE Email=@Email AND Password=@Password AND Status!=@Status";


    using (SqlCommand cmd = new SqlCommand(query, con))
    {
        cmd.Parameters.AddWithValue("@Email", record.Email);
        cmd.Parameters.AddWithValue("@Password", record.Password);
        cmd.Parameters.AddWithValue("@Status", "Suspended");

        using (SqlDataReader data = cmd.ExecuteReader())
        {
            if (data.HasRows)
            {

                while (data.Read())
                {
                    Session["userid"] = data["UserID"].ToString();
                    Session["typeid"] = data["TypeID"].ToString();
                }

                if ((string)Session["typeid"] == "Student")

                    sRedirecttoAction = "Profile";

                if ((string)Session["typeid"] == "Tutor")

                    sRedirecttoAction = "TutorProfile";
            }
            else
            {
                ViewBag.Error = "<div class = alert alert-danger col-lg-6'>Invalid Credentials.</div>";
                return View();
            }
        }
    }
}

//return RedirectToAction here.
return RedirectToAction(sRedirecttoAction);

#3


0  

The Compiler Error CS0161 "not all code paths return value" is referring to the commented portion I've noted below as // HERE.

编译器错误CS0161“并非所有代码路径都返回值”是指我在下面注明的注释部分//这里。

It's saying, if the two IF conditions are FALSE, what am I supposed to return?

它说,如果两个IF条件都是FALSE,我应该返回什么?

I've wrapped braces around the statements to help describe what I mean.

我在声明中围绕着括号来帮助描述我的意思。

if (data.HasRows)
{
    while (data.Read())
    {
        Session["userid"] = data["UserID"].ToString();
        Session["typeid"] = data["TypeID"].ToString();
    }

    if ((string)Session["typeid"] == "Student")
    {
        return RedirectToAction("Profile");
    }

    if ((string)Session["typeid"] == "Tutor")
    {
        return RedirectToAction("TutorProfile");
    }

    // HERE
}

#4


-1  

The error appears to be in the code below. I revised that part of your code. Try that and see if that works

错误似乎在下面的代码中。我修改了你的部分代码。试试看,看看是否有效

if (data.HasRows)
                {

                    while (data.Read())
                    {
                        Session["userid"] = data["UserID"].ToString();
                        Session["typeid"] = data["TypeID"].ToString();
                    }

                    //make sure there is no space between these two lines
                    if ((string)Session["typeid"] == "Student")
                      return RedirectToAction("Profile");

                    //make sure there is no space between these two lines
                    if ((string)Session["typeid"] == "Tutor")
                        return RedirectToAction("TutorProfile");

                    //###if those two if clauses are not met, there is no return after that.
                    return view(); //? or something else
                }

#1


0  

What if typeid is neither Student or Tutor?

如果typeid既不是学生也不是导师怎么办?

Consider either:

if ((string)Session["typeid"] == "Student")
    return RedirectToAction("Profile");
else if ((string)Session["typeid"] == "Tutor")
    return RedirectToAction("TutorProfile");

or:

if ((string)Session["typeid"] == "Student")
    return RedirectToAction("Profile");
if ((string)Session["typeid"] == "Tutor")
    return RedirectToAction("TutorProfile");
ViewBag.Error = "<div class = alert alert-danger col-lg-6'>Invalid User Type.</div>";
return View();

#2


0  

Look the changes below.

查看下面的更改。

string sRedirecttoAction = String.Empty;

string sRedirecttoAction = String.Empty;

using (SqlConnection con = new SqlConnection(Helper.GetConnection()))
{
    con.Open();
    string query = @"SELECT UserID, TypeID FROM Users WHERE Email=@Email AND Password=@Password AND Status!=@Status";


    using (SqlCommand cmd = new SqlCommand(query, con))
    {
        cmd.Parameters.AddWithValue("@Email", record.Email);
        cmd.Parameters.AddWithValue("@Password", record.Password);
        cmd.Parameters.AddWithValue("@Status", "Suspended");

        using (SqlDataReader data = cmd.ExecuteReader())
        {
            if (data.HasRows)
            {

                while (data.Read())
                {
                    Session["userid"] = data["UserID"].ToString();
                    Session["typeid"] = data["TypeID"].ToString();
                }

                if ((string)Session["typeid"] == "Student")

                    sRedirecttoAction = "Profile";

                if ((string)Session["typeid"] == "Tutor")

                    sRedirecttoAction = "TutorProfile";
            }
            else
            {
                ViewBag.Error = "<div class = alert alert-danger col-lg-6'>Invalid Credentials.</div>";
                return View();
            }
        }
    }
}

//return RedirectToAction here.
return RedirectToAction(sRedirecttoAction);

#3


0  

The Compiler Error CS0161 "not all code paths return value" is referring to the commented portion I've noted below as // HERE.

编译器错误CS0161“并非所有代码路径都返回值”是指我在下面注明的注释部分//这里。

It's saying, if the two IF conditions are FALSE, what am I supposed to return?

它说,如果两个IF条件都是FALSE,我应该返回什么?

I've wrapped braces around the statements to help describe what I mean.

我在声明中围绕着括号来帮助描述我的意思。

if (data.HasRows)
{
    while (data.Read())
    {
        Session["userid"] = data["UserID"].ToString();
        Session["typeid"] = data["TypeID"].ToString();
    }

    if ((string)Session["typeid"] == "Student")
    {
        return RedirectToAction("Profile");
    }

    if ((string)Session["typeid"] == "Tutor")
    {
        return RedirectToAction("TutorProfile");
    }

    // HERE
}

#4


-1  

The error appears to be in the code below. I revised that part of your code. Try that and see if that works

错误似乎在下面的代码中。我修改了你的部分代码。试试看,看看是否有效

if (data.HasRows)
                {

                    while (data.Read())
                    {
                        Session["userid"] = data["UserID"].ToString();
                        Session["typeid"] = data["TypeID"].ToString();
                    }

                    //make sure there is no space between these two lines
                    if ((string)Session["typeid"] == "Student")
                      return RedirectToAction("Profile");

                    //make sure there is no space between these two lines
                    if ((string)Session["typeid"] == "Tutor")
                        return RedirectToAction("TutorProfile");

                    //###if those two if clauses are not met, there is no return after that.
                    return view(); //? or something else
                }