程序代码如下:
public void Vote(int answer){

if(HttpContext.Current!=null)
{
if(HttpContext.Current.Response.Cookies["WroxPoll"]!=null)
{
Console.WriteLine("find cookies");
if(HttpContext.Current.Response.Cookies["WroxPoll"].Value==myQuestion)
{
Console.WriteLine("cookies' value=="+myQuestion);
throw (new Exception("您已经对该问题进行了投票!"));
}
}
}
DataSet dataSet = new DataSet();
FileStream fsXmlReader = new FileStream(myFilename,FileMode.Open);
XmlTextReader xmlReader = new XmlTextReader(fsXmlReader);
dataSet.ReadXml(xmlReader);
xmlReader.Close(); string vote = dataSet.Tables[1].Rows[answer-1].ItemArray[1].ToString();
int voteInt = int.Parse(vote);
DataRow dataRow = dataSet.Tables[1].Rows[answer-1];
dataRow["Votes"] = (voteInt+1).ToString();
myVotes[answer] = voteInt+1; StreamWriter streamWriter = new StreamWriter(myFilename);
dataSet.WriteXml(streamWriter,XmlWriteMode.IgnoreSchema);
streamWriter.Close(); if(HttpContext.Current!=null){
HttpCookie myCookie = new HttpCookie("WroxPoll",myQuestion);
HttpContext.Current.Response.Cookies.Add(myCookie);
}
}其功能就是在第一次调用该函数的时候,建立一个cookie;当再次调用该函数的时候,就检查该cookie存不存在,值是否为建立时指定的值,如果是就抛出异常。但是我调式的时候发现,在浏览器中打开相应页面(在html中的page标签中加上了Trace="true"),Cookie 集合中显示没有WroxPoll这个我定义的cookie;但是在后台程序中第一次判断该cookies是否为null时,结果是不为null;在函数的最后一句执行完后,监视HttpContext.Current.Response.Cookies["WroxPoll"].Value的值还是null;在函数执行完返回页面时,Cookie 集合中显示多了一个名为WroxPoll的cookie,值也是正常的;但是当再次运行到函数体的时候,HttpContext.Current.Response.Cookies["WroxPoll"].Value的值却是null.请问这是什么原因呢?