ASP.NET MVC、私はこのようなキーをいくつかの重要な、クッキーの値、取得カントクッキー値(NameValueCollectionの) - 私はクッキーを追加するためにbeloweコードを使用
public static void AddCookie(this HttpContextBase httpContextBase, string cookieName, NameValueCollection cookieValues, DateTime expires, bool httpOnly = false)
{
var cookie = new HttpCookie(cookieName)
{
Expires = expires,
//Value = httpContextBase.Server.UrlEncode(value),// For Cookies and Unicode characters
HttpOnly = httpOnly
};
cookie.Values.Add(cookieValues);
//httpContextBase.Response.Cookies.Add(cookie);
System.Web.HttpContext.Current.Response.Cookies.Add(cookie);
}
を追加して入力します。
NameValueCollection CookieValues = new NameValueCollection();
CookieValues.Add("pid", shoppingCartViewModel.ProductId.ToString());
CookieValues.Add("qty", "1");
HttpContext.AddCookie(shoppingCartCookiName, CookieValues, DateTime.Now.AddDays(1));
読み取りCookieを使用する場合、値はnullです。 私はクッキー値
public static NameValueCollection GetCookieValues(this HttpContextBase httpContext, string cookieName)
{
var cookie = System.Web.HttpContext.Current.Response.Cookies[cookieName];
if (cookie == null)
return null; //cookie doesn't exist
// For Cookies and Unicode characters
return cookie.Values;
}