テキスト ファイルに文字列があり、それを Cookie として保存したいと考えています。ただし、私のコードはSystem.NullReferenceException
. これを修正するにはどうすればよいですか?
これが私のコードです:
public static bool SetCookie(string cookiename, string cookievalue, int iDaysToExpire)
{
try
{
HttpCookie objCookie = new HttpCookie(cookiename);
HttpContext.Current.Response.Cookies.Clear();
HttpContext.Current.Response.Cookies.Set(objCookie);
objCookie.Values.Add(cookiename, cookievalue);
DateTime dtExpiry = DateTime.Now.AddDays(iDaysToExpire);
HttpContext.Current.Response.Cookies[cookiename].Expires = dtExpiry;
}
catch (Exception e)
{
System.Console.WriteLine(e);
return false;
}
return true;
}