views:

316

answers:

2

I am having a problem where a cookie I am setting is being lost directly after RedirectToAction() is called. Is something going on behind the scenes that invalidates the current request and creates a new one that causes the cookie to be lost before it has been persisted to disk?

I understand that if you want data to be available after the redirect you need to use TempData, but is that the same for cookies? If so, doesn't that seem ugly to have to store the cookie value in TempData and then write the cookie later?

Update:

I just realized that the cookie is lost at the end of the request, it doesn't matter if I call RedirectToAction(). So now the question is why wont the cookie persist accros two requests? (I update the code below to show what I am doing now)

public ActionResult DoSomething()
{
   Response.Cookies["SomeCookie"].Value = "Jarified";
   Response.Cookies["SomeCookie"].Expires = DateTime.UtcNow.AddDays(3);

   return View("SomeView");

}


Update

I created a new MVC project using the default template. I modified the HomeController/Index action to have the code below. The first time I hit the view "Cookie Not Found" is printed as expected. However, every subsequent time the same message is printed. If I remove the line that sets the expiration date then everything works just fine. I guess the real question here is why does making the cookie persistent cause the browser to throw it away? Is there a trick to making cookies persistent in MVC?

    public ActionResult Index()
    {
        HttpCookie cookie = Request.Cookies["temp"];
        if (cookie == null)
        {
            ViewData["Message"] = "Cookie Not Found";
            Response.Cookies["temp"].Value = "Welcome to ASP.NET MVC!";
            Response.Cookies["temp"].Expires = DateTime.UtcNow;
        }
        else
        {
            ViewData["Message"] = cookie.Value;
        }
        return View();
    }
+1  A: 

Take a look at this article. It should address your issue!

http://stephenwalther.com/blog/archive/2008/07/08/asp-net-mvc-tip-15-pass-browser-cookies-and-server-variables-as-action-parameters.aspx

    var newCookie = new HttpCookie("myCookie", cookieValue);
    newCookie.Expires = DateTime.Now.AddDays(10);
    Response.AppendCookie(newCookie);
    return RedirectToAction("Index");
Andrew Siemer
+2  A: 

The reason that

Response.Cookies["temp"].Expires = DateTime.UtcNow;

is not working is that it sets a cookie with an expires in the past (at least for me).

It works when I change it to

Response.Cookies["temp"].Expires = DateTime.UtcNow.AddDays(3);

Setting a cookie expires property to the past will delete the cookie.

This code works for me:

    public ActionResult Index() {
        HttpCookie cookie = Request.Cookies["temp"];
        if (cookie == null) {
            ViewData["Message"] = "Cookie Not Found";
            Response.Cookies["temp"].Value = "This is a cookie: Welcome to ASP.NET MVC!";
            Response.Cookies["temp"].Expires = DateTime.UtcNow.AddDays(3);
        } else {
            return RedirectToAction("Something");
        }
        return View();
    }

    public ActionResult Something() {
        HttpCookie cookie = Request.Cookies["temp"];
        ViewData["Message"] = cookie.Value;
        return View();
    }
jao
Late last night I realized that If I changed the second sample to DateTime.UtcNow.AddDays(3); it was working. What I am still unclear about is why my first example wont work for me. I think what my original problem was is the value I was storing -it ins't "Jarified" but a much longer string. I wonder if the expiration date is causeing the length to be over 4K. Thanks for the answer!
Joe