【问题标题】:Redirect from a [HttpGet] Action To [HttpPost] Action - MVC3从 [HttpGet] 动作重定向到 [HttpPost] 动作 - MVC3
【发布时间】:2013-01-17 13:55:31
【问题描述】:

我想从同一控制器中的 [HttpGet] Action 重定向到 [HttpPost] Action。

有可能吗?

我试图不为登录重写相同的代码,因为它是一个复杂的,而不是典型的登录函数

这是我的代码:

[HttpGet]
public ActionResult Login(){
    return View();
}

[HttpPost]
public ActionResult Login(LoginModel model)
{
    //search user and create sesion
    //...

    if (registered)
        {
        return this.RedirectToAction("Home","Index");                      
        }else{
        return this.RedirectToAction("Home", "signIn");
    }
}

[HttpGet]
public ActionResult signIn(){
    return View();
}

[HttpGet]
public ActionResult signInUserModel model)
{
    //Register new User
    //...
    if (allOk)
        {
        LoginModel loginModel = new LoginModel();
            loginModel.User = model.User;
            loginModel.password = model.password;

            //next line doesn't work!!!!!
        return this.RedirectToAction("Home","Login", new { model = loginModel); 

        }else{
        //error
        //...

    }

}

任何帮助将不胜感激。

谢谢

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-3 web-applications razor web


    【解决方案1】:

    你可以从方法名返回不同的视图

    public ActionResult signInUserModel model)
    {
        ...
        return View("Login", loginModel);
    }
    

    有点晚了,但我也遇到了同样的问题...

    【讨论】:

      【解决方案2】:

      您可以从 Post 方法中重构核心登录逻辑,然后从两个位置调用该新方法。

      例如假设您创建了一个 LoginService 类来处理某人登录。这只是在您的两个操作中都使用它的一种情况,因此无需从一个操作重定向到另一个操作

      【讨论】:

        【解决方案3】:

        这是可能的。所有动作都必须在同一个控制器中。

        public ActionResult Login(){
            return View();
        }
        
        [HttpPost]
        public ActionResult Login(LoginModel model)
        {
            //search user and create sesion
            //...
        
            if (registered)
            {
                return RedirectToAction("Index");
            }
        
            return View(model);
        }
        
        public ActionResult SignIn(){
            return View();
        }
        
        [HttpPost]
        public ActionResult SignIn(UserModel model)
        {
            //Register new User
            //...
            if (allOk)
            {
                return RedirectToAction("Login");
            }
        
            return View(model);
        }
        

        【讨论】:

        • 是的,但是我试图只显示一次登录页面,因为用户不输入用户名和密码,而是使用磁卡。如果用户未注册,“登录”过程是自动的,在后台
        猜你喜欢
        • 2011-09-25
        • 1970-01-01
        • 1970-01-01
        • 2017-10-28
        • 2012-07-30
        • 1970-01-01
        • 2015-07-06
        • 2018-10-17
        • 2014-11-26
        相关资源
        最近更新 更多