Asp.net mvc 从[HttpGet]操作重定向到[HttpPost]操作-MVC3

Asp.net mvc 从[HttpGet]操作重定向到[HttpPost]操作-MVC3,asp.net-mvc,asp.net-mvc-3,web-applications,razor,web,Asp.net Mvc,Asp.net Mvc 3,Web Applications,Razor,Web,我想从同一控制器中的[HttpGet]操作重定向到[HttpPost]操作 public ActionResult Login(){ return View(); } [HttpPost] public ActionResult Login(LoginModel model) { //search user and create sesion //... if (registered) { return RedirectToAction

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

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);
}
可能吗

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

这是我的代码:

[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
        //...

    }

}
任何帮助都将不胜感激


谢谢

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


e、 假设您创建了一个LoginService类来处理某人的登录。这只是在两个动作中都使用它的情况,因此不需要从一个动作重定向到另一个动作,这是可能的。所有操作必须在同一控制器中

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);
}

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

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

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

是的,但我只尝试显示一次登录页面,因为用户不输入用户名和密码,而是使用磁卡。如果用户未注册,“登录”过程在后台自动进行