控制器仅返回1个视图,而不选择正确的视图

时间:2018-06-21 02:56:39

标签: c# asp.net-core-mvc asp.net-core-2.0

我有2个控制器,它们都显示相同的视图,第一个显示特定的SSO提供程序,另一个显示所有的SSO提供程序。发布登录表单时出了点问题,[HttpPost]控制器返回/ account / login或/ account / login?code = Facebook,但两者均为第二视图。如果代码不为空,是否可以返回/ account / {code} / login视图,如果是,则返回另一个视图?无论返回哪个视图,我都需要模型以及返回视图时要保留的所有错误。

[HttpGet]
[Route("/account/{code}/login")]
[AllowAnonymous]
public async Task<IActionResult> Login(string code, string returnUrl = null)
{

    ViewData["ReturnUrl"] = returnUrl;
    ViewData["Code"] = code;
    LoginViewModel model = new LoginViewModel();
    model.SocialLogins = new List<string> { code };
    return View(model);
}
[HttpGet]
[Route("/account/login")]
[AllowAnonymous]
public async Task<IActionResult> Login(string returnUrl = null)
{
    ViewData["ReturnUrl"] = returnUrl;
    LoginViewModel model = new LoginViewModel();
    model.SocialLogins = _socialLogins;
    return View(model);
}

[HttpPost]
[AllowAnonymous]
[Route("/account/login")]
[Route("/account/{code}/login")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Login(LoginViewModel model, string code = null, string returnUrl = null)
{
    ViewData["Code"] = code;
    ViewData["ReturnUrl"] = returnUrl;
    if (ModelState.IsValid)
    {
        //login code
    }

    return View(model);
}

public class LoginViewModel
{
    [Required]
    [EmailAddress]
    public string Email { get; set; }

    [Required]
    [DataType(DataType.Password)]
    public string Password { get; set; }

    [Display(Name = "Remember me?")]
    public bool RememberMe { get; set; }

    public List<string> SocialLogins { get; set; }
}

0 个答案:

没有答案