C#持久性cookie
我在stackoverflow上看到了ASP.NET MVC#中的持久cookie示例。 但是我不明白为什么下面的代码不起作用 首先,我向cookie写入:C#持久性cookie,c#,razor,cookies,C#,Razor,Cookies,我在stackoverflow上看到了ASP.NET MVC#中的持久cookie示例。 但是我不明白为什么下面的代码不起作用 首先,我向cookie写入: HttpCookie cookie = new HttpCookie("AdminPrintModule"); cookie.Expires = DateTime.Now.AddMonths(36); cookie.Values.Add("PrinterSetting1", Request.QueryString["Printer1"])
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中的cookies。内容看起来不错
然后读取代码:
HttpCookie cookie = Request.Cookies["AdminPrintModule"];
test = cookie.Values["PrinterSetting2"].ToString();
cookie变量保持为null。在测试变量中存储PrinterSetting2值失败
我不知道我做错了什么,因为这或多或少是stackoverflow示例中的复制粘贴。为什么我不能从cookie中读取PrinterSetting2值?请尝试以下代码:-
if (Request.Cookies["AdminPrintModule"] != null)
{
HttpCookie cookie = Request.Cookies["AdminPrintModule"];
test = cookie["PrinterSetting2"].ToString();
}
请查看此文档:-
以下是几种写入和读取cookie的类型:-
非持久Cookie-Cookie已过期,称为
非持久Cookie
如何创建cookie?在计算机中创建cookie非常容易
借助响应对象或HttpCookie的Asp.Net
例1:
HttpCookie userInfo = new HttpCookie("userInfo");
userInfo["UserName"] = "Annathurai";
userInfo["UserColor"] = "Black";
userInfo.Expires.Add(new TimeSpan(0, 1, 0));
Response.Cookies.Add(userInfo);
string User_Name = string.Empty;
string User_Color = string.Empty;
User_Name = Request.Cookies["userName"].Value;
User_Color = Request.Cookies["userColor"].Value;
例2:
Response.Cookies["userName"].Value = "Annathurai";
Response.Cookies["userColor"].Value = "Black";
string User_name = string.Empty;
string User_color = string.Empty;
HttpCookie reqCookies = Request.Cookies["userInfo"];
if (reqCookies != null)
{
User_name = reqCookies["UserName"].ToString();
User_color = reqCookies["UserColor"].ToString();
}
如何从cookie中检索?
它是通过请求从cookes中检索cookie值的简单方法
对象例1:
HttpCookie userInfo = new HttpCookie("userInfo");
userInfo["UserName"] = "Annathurai";
userInfo["UserColor"] = "Black";
userInfo.Expires.Add(new TimeSpan(0, 1, 0));
Response.Cookies.Add(userInfo);
string User_Name = string.Empty;
string User_Color = string.Empty;
User_Name = Request.Cookies["userName"].Value;
User_Color = Request.Cookies["userColor"].Value;
例2:
Response.Cookies["userName"].Value = "Annathurai";
Response.Cookies["userColor"].Value = "Black";
string User_name = string.Empty;
string User_color = string.Empty;
HttpCookie reqCookies = Request.Cookies["userInfo"];
if (reqCookies != null)
{
User_name = reqCookies["UserName"].ToString();
User_color = reqCookies["UserColor"].ToString();
}
您必须确保Request.QueryString.com中有值,以检查代码是否工作硬编码cookie的值,然后从cookie中读取。我理解这可以防止失败,但它不能解决我从cookie中读取PrinterSetting2值的问题。为什么我不能读取此值?请不要像我在示例@Walt501I中所做的那样尝试输入值[“”],而是在退出函数并检查值之前复制粘贴您的示例1,但要设置断点。编写cookie很顺利,我可以在调试模式下看到所有的值。读取仍然会传递一个空值,我在调试模式下也可以看到这一点。我快发疯了!我可以用记事本检查cookie的内容。这些值按预期存储。但我似乎看不懂它们。我复制了你们的代码,工作正常,我可以从cookie中读取值。