如何在MVC3/Razor中正确重定向(同时设置cookie)?

Nei*_*l N 5 c# asp.net-mvc redirect asp.net-mvc-3

首先,我感觉Response.Redirect只是经典ASP的遗留物,我应该在MVC范例中使用其他东西.

第二,虽然我当前的Response.Redirect IS实现工作,但它没有设置我想要的cookie.我假设这是因为标头被删除而不是重定向发送到客户端.

这是我到目前为止:

    [HttpPost]
    public ActionResult Login(FormCollection form)
    {
        User user;
        string sessionKey;

        if (UserManager.Login(form["Email"], form["Password"]))
        {
            // Login stuff here

            // Remember user's email
            Response.Cookies["Email"].Value = form["Email"];
            Response.Cookies["Email"].Expires = DateTime.Now.AddDays(31);

            // Redirect to homepage
            Response.Redirect("~/");
        }
     }
Run Code Online (Sandbox Code Playgroud)

SLa*_*aks 8

在MVC中重定向的正确方法是return RedirectToAction("Home", "Index").

cookie应该有效.