开发者

Problem saving/getting cookies in MVC 2

Try to save them like this:

HttpCookie latcook = new HttpCookie("latitude", lat.Value.ToString());
                HttpCookie lngcook = new HttpCookie("longitude", lng.Value.ToString());
                Request.Cookies.Add(latcook);
                Request.Cook开发者_JS百科ies.Add(lngcook);

Everything has a value, and the code steps through without error.

Then immediately after those are set, I refresh my page and step through this:

HttpCookie latcook = Request.Cookies.Get("latitude");
                HttpCookie lngcook = Request.Cookies.Get("longitude");

The latcook and lngcook variables have names, but no values. What am I doing wrong?


You are adding your cookies to the request object. They should be added to the response:

Response.Cookies.Add(latcook);
Response.Cookies.Add(lngcook);

Cookies added to the response are returned to the user's browser via a series of Set-Cookie HTTP headers. They are then subsequently sent back (upon the next request) via the Cookie HTTP header. (You should be able to watch this happen using Firebug, etc.) Ultimately, this header will be parsed and populate the Request.Cookies collection.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜