How to redirect (when setting a cookie) in MVC3 / Razor?

Firstly, I get the feeling that Response.Redirect just stays from the classic ASP, and I have to use something else in the MVC paradigm.

And secondly, although my current implementation of Response.Redirect IS is working, it does not set the cookie I want. I assume this is because the header is being deleted, not sent to the client when redirecting.

Here is what I still have:

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

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

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

            // Redirect to homepage
            Response.Redirect("~/");
        }
     }
+5
source share
1 answer

The correct way to redirect in MVC return RedirectToAction("Home", "Index").

Cookies should work.

+8
source

All Articles