【问题标题】:Why does ActionResult throw error if I don't return a view ?如果我不返回视图,为什么 ActionResult 会抛出错误?
【发布时间】:2017-11-03 12:32:44
【问题描述】:

如果条件为真,则重定向,否则返回当前视图。这是我打算做的,但我只得到了第一部分而不是第二部分,比如返回一个视图,因为如果我把返回视图放在 else 部分,那么 ActionResult 会抛出一个错误,因为它需要返回一些东西。

 public ActionResult Authenticate(Users u)
        {
            if (basicOps.getUsersLogin(u.UserName, u.Password)) 
            {
                RedirectToAction("GetImagesStories", "Stories");

            }

            return View("Authenticate");


        }

这样我无法重定向,因为它总是执行返回部分,但我希望它仅在 IF 条件失败时运行。

【问题讨论】:

    标签: c# asp.net asp.net-mvc asp.net-mvc-4 asp.net-web-api


    【解决方案1】:

    您忘记返回RedirectToAction 操作结果:

        public ActionResult Authenticate(Users u)
        {
            if (basicOps.getUsersLogin(u.UserName, u.Password)) 
            {
                return RedirectToAction("GetImagesStories", "Stories");
            }
    
            return View("Authenticate");
        }
    

    【讨论】:

    • 哈哈谢谢。微软太有趣了,为什么要在 RedirectToAction 之前返回并且没有任何错误。哈哈
    猜你喜欢
    • 1970-01-01
    • 2021-11-22
    • 1970-01-01
    • 2019-03-12
    • 1970-01-01
    • 2020-05-15
    • 1970-01-01
    • 1970-01-01
    • 2016-11-28
    相关资源
    最近更新 更多