ASP.NET MVC 4中的自定义重定向

时间:2013-12-26 08:37:09

标签: asp.net asp.net-mvc asp.net-mvc-4 simplemembership

我正在尝试为我的ASP.NET MVC 4应用程序中的用户构建两步自定义注册。这涉及在注册后将用户重定向到用户控制器中的“编辑”视图。我已经调整了股票账户控制器模板中的代码来实现这一目标。

 public ActionResult Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                // Attempt to register the user
                try
                {
                    WebSecurity.CreateUserAndAccount(model.UserName, model.Password);
                    WebSecurity.Login(model.UserName, model.Password);
                    return RedirectToAction("Edit", "User",  new { id = WebSecurity.CurrentUserId });
                }
                catch (MembershipCreateUserException e)
                {
                    ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
                }
            }


            return View(model);
        }

但是当我运行它时,我会在显示的地址栏上显示404

http://localhost:17005/User/Edit/-1

这意味着它将进入用户控制器的编辑操作,但最后以Id为-1结束。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

Membership.GetUser().ProviderUserKey

或者,您无法将用户ID传递给编辑操作,并让代码使用当前登录的用户。

RedirectToAction("Edit", "User");

在您的用户控制器中

[Authorize]
public ActionResult Edit()
{
    object userId = Membership.GetUser().ProviderUserKey;
}

请原谅我目前在手机上的简洁