1

我在文本文件中有字符串,我想将其保存为 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;
}
4

0 回答 0