我已經在stackoverflow上看到了ASP.NET MVC C#中的持久性cookie示例。 但我不明白爲什麼下面的代碼不起作用。C#持久性cookie
首先,我寫的cookie:
HttpCookie cookie = new HttpCookie("AdminPrintModule");
cookie.Expires = DateTime.Now.AddMonths(36);
cookie.Values.Add("PrinterSetting1", Request.QueryString["Printer1"]);
cookie.Values.Add("PrinterSetting2", Request.QueryString["Printer2"]);
cookie.Values.Add("PrinterSetting3", Request.QueryString["Printer3"]);
Response.Cookies.Add(cookie);
我看到存儲在Internet Explorer中的cookie。內容看起來不錯。
然後閱讀代碼:
HttpCookie cookie = Request.Cookies["AdminPrintModule"];
test = cookie.Values["PrinterSetting2"].ToString();
的cookie變量保持空。在測試變量中存儲PrinterSetting2值失敗。
我不知道我在做什麼錯,因爲這或多或少是一個複製粘貼從這裏的例子在stackoverflow。爲什麼我無法從Cookie中讀取PrinterSetting2值?
我知道這可以防止失敗,但它不能解決我從Cookie中讀取PrinterSetting2值的問題。爲什麼我不能讀取這個值? – Walt501 2014-09-05 10:36:59
我已經更新了我的答案看看@ Walt501 – Neel 2014-09-05 10:41:39
而不是將值[「」]嘗試像我在我的例子中所做的一樣@ Walt501 – Neel 2014-09-05 10:42:36