MVC 2 - Cookie 不保存
我有一个简单的应用程序,我试图将一个值保存到我的 cookie 中,但它没有保存。下面是代码,不知道问题出在哪里。
下面的代码来自控制器:
public ActionResult Index()
{
string cookieValue = "";
if (Request.Cookies["my_cookie"] != null)
{
cookieValue = Request.Cookies["my_cookie"].Value;
}
if (! string.IsNullOrEmpty(cookieValue ))
{
ViewData["ck"] = cookieValue;
}
else { ViewData["ck"] = "no cookie value"; }
return View();
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult SaveCookieData(FormCollection formValue)
{
HttpCookie myCookie = new HttpCookie("my_cookie", formValue["cookieTXT"].ToString());
Response.Cookies.Add(myCookie);
}
此代码来自视图:
<% using (Html.BeginForm("SaveCookieData", "Home", FormMethod.Post)) { %>
<textarea id="cookieTXT" rows="2" cols="20" runat="server" />
<input id="submitBTN" type="submit" value="Done" runat="server" />
<% } %>
<% if (ViewData["ck"] != null) { %>
<p>Hello Cookie: <%= ViewData["ck"]%></p>
<% } %>
从看起来我的表单提交的 cookieTXT 数据为空,因为 formValue["cookieTXT"] 的值为空。我不明白为什么?
感谢您的帮助。
I have a simple application where I am trying to save a value into my cookie but it is not saving. Below is the code and I do not know where the problem is.
Code below is from the controller:
public ActionResult Index()
{
string cookieValue = "";
if (Request.Cookies["my_cookie"] != null)
{
cookieValue = Request.Cookies["my_cookie"].Value;
}
if (! string.IsNullOrEmpty(cookieValue ))
{
ViewData["ck"] = cookieValue;
}
else { ViewData["ck"] = "no cookie value"; }
return View();
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult SaveCookieData(FormCollection formValue)
{
HttpCookie myCookie = new HttpCookie("my_cookie", formValue["cookieTXT"].ToString());
Response.Cookies.Add(myCookie);
}
This code is from the view:
<% using (Html.BeginForm("SaveCookieData", "Home", FormMethod.Post)) { %>
<textarea id="cookieTXT" rows="2" cols="20" runat="server" />
<input id="submitBTN" type="submit" value="Done" runat="server" />
<% } %>
<% if (ViewData["ck"] != null) { %>
<p>Hello Cookie: <%= ViewData["ck"]%></p>
<% } %>
From what it looks like is my form submitted data for cookieTXT is empty because the value for formValue["cookieTXT"] is blank. I can not figure it out why?
Thanks for the help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
第一:尝试使用
Response.SetCookie(myCookie);
第二:
您是否尝试过查看浏览器中保存的 cookie?我使用 Google Chrome 来查看浏览器正在读取的 cookie。您还可以使用 fiddler 查看响应中的 Set-Cookie: 来查看其是否正确响应。另外,请确保设置 cookie 的域与读取 cookie 的域匹配。
前任。 Domain = domain.com
您的站点的 URL 中必须包含 domain.com 才能读取 cookie。
First: Try using
Response.SetCookie(myCookie);
Second:
Have you tried looking at the cookie saved in the browser? I use Google Chrome to see the cookies that are being read by the browser. You can also use fiddler to see the Set-Cookie: in the response to see if it is responding correctly. Also, make sure the domain setting the cookie matches the domain that is reading the cookie.
Ex. Domain = domain.com
Your site must have domain.com in the URL to read the cookie.