【问题标题】:Not all paths return a value in Public ActionResult , I used Exception并非所有路径都在 Public ActionResult 中返回值,我使用了 Exception
【发布时间】:2019-10-04 14:54:32
【问题描述】:

基本上当我这样做时

public ActionResult ValidateLogin()
{
    return View();
}

现在工作正常当我在这种情况下使用 try catch 块并且我在 try catch 块中执行类似 return RedirectToAction("Dashboard"); 的操作时,我还希望它应该检查指定,然后再将用户重定向到主页,它触发和错误,就像并非所有路径都返回一个值。

我的源代码是这样的

public ActionResult ValidateLogin(UserAuthClass auth)
{
    string constring = @"Data Source=DESKTOP-9CM4N5S\SQLEXPRESS;Initial Catalog=MVCLogintestDB;Integrated Security=True";
    using (SqlConnection con = new SqlConnection(constring))
    {
        con.Open();

        string query = "select * from [MVCLogintestDB].[dbo].[users_table] where username = @username and password= @password and designation = @designation";
        using (SqlCommand cmd = new SqlCommand(query, con))
        {
            cmd.Parameters.AddWithValue("@username",auth.username);
            cmd.Parameters.AddWithValue("@password", auth.password);
            cmd.Parameters.AddWithValue("designation", auth.designation);

            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();

            try
            {
                da.Fill(dt);
                if (dt.Rows.Count > 0)
                {
                    if (auth.designation == "Admin")
                    {
                        return RedirectToAction("AdminDashboard");
                    }
                    else if (auth.designation == "Security")
                    {
                        return RedirectToAction("SecurityDashboard");
                    }
                    else if (auth.designation == "Visitor")
                    {
                        return RedirectToAction("VisitorDashboard");
                    }
                }
                else
                {
                    return RedirectToAction("WrongPasswordArea");
                }
            }
            catch(Exception ex)
            {
                throw ex;
            }
        }
    }
}

我试图了解我在这里似乎遗漏了什么错误的东西?

【问题讨论】:

  • dt.Rows.Count > 0 为真,并且auth.designation 既不是"Admin" 也不是"Security" 也不是"Visitor" ,那么您当前的代码不会返回任何内容。这可能就是编译器所抱怨的。
  • @bassfader,我不明白
  • 您缺少return 声明,因为dt.Rows.Count > 0auth.designation 不是“Admin”“Security” i> 或“访客”。对于方法中所有可能的代码路径,您的代码没有 return 语句。例如,当auth.designation 是其他东西时,您想做什么,例如 "Guest"
  • 看起来您混淆了业务和表示逻辑。正如 Jerry 在他的回答中所说,您必须重定向到一个操作并向用户显示错误,try-catch 引入了另一个不返回视图的代码路径。这就是为什么遵循 MVC 模式来添加抽象并清楚地了解 Controller 的职责从哪里开始和结束是很好的原因。
  • @bassfader,谢谢兄弟,你做得很好,我非常感激,但杰瑞说对了。完全按照我的意愿工作

标签: c# sql-server asp.net-mvc


【解决方案1】:

重构您的 if-else-if 逻辑。我实现了一个更简单的版本,结果你的错误消失了。

你必须适应,就像我在快速环境中测试它一样。但只要勇敢一点,if-else-if,让它更合乎逻辑。在下面的代码中再次没有关于缺少返回的错误。

public string ValidateLogin(string auth)
{
    using (new NoDispose())
    {
        using (new NoDispose())
        {
            try
            {
                int dtRowsCount = 10;
                string authDesignation = "";
                if (dtRowsCount <= 0)
                    return "WrongPasswordArea";
                switch (authDesignation)
                {
                    case "Admin":
                        return "AdminDashboard";
                    case "Security":
                        return "SecurityDashboard";
                    case "SecurityDashboard":
                        return "SecurityDashboard";
                    case "Visitor":
                    default:
                        return "VisitorDashboard";
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}

免责声明。我并不是说上面实现的逻辑完全符合前面的逻辑,也不符合您的要求。

使用链

顺便说一句,您可以通过以下方式链接using 语句。

using (var a = new NoDispose())
using (var b = new NoDispose())
{
    // both a and b are available in here
}

【讨论】:

    【解决方案2】:

    您需要在 using 块之外有一个默认返回。 像这样的

    return RedirectToAction("Login");
    

    当所有方案都失败时,将用户返回到登录页面或任何您的登录页面。

    【讨论】:

    • 在 using(SqlConnection con = new SqlConnection(constring)) 之外你的意思是?如果是,我做到了,到目前为止我没有看到任何错误
    • 我强烈建议不要使用这种默认行为,因为这可能只会隐藏潜在的未来错误。例如,当引入了一个新的可能的designation 值,但上面的 if-statemtents 没有相应地调整,那么这将“静默失败”并重定向到登录页面。
    • @Jerry,非常感谢,按我的预期工作,redirectAction("Login") 可以做什么。
    【解决方案3】:

    在您的捕获中设置回报。我认为这对你来说会更好,而不是在你的 using 块之外返回,因为从你 using 的返回中,你可以开始显示来自这个返回的错误,而不是看起来像什么都没发生的一般返回。

    【讨论】:

      猜你喜欢
      • 2016-06-01
      • 1970-01-01
      • 2011-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多