2010-09-23 61 views
1

在我的内容页我的代码(在Page_Load中):C#饼干未写入

if (Master.pageAction == "remove") 
    { 
     int removeProductID = int.Parse(Request.QueryString["ID"]); 
     int removeOptionID = int.Parse(Request.QueryString["optID"]); 
     Master.myBasket.removeFromBasket(removeProductID, removeOptionID); 
     //Response.Redirect("viewBasket.aspx"); 
    } 

功能从篮子中删除被定义为:

// Removes item from a basket 
public void removeFromBasket(int itemsID, int optionsID) 
{ 
    Page myPage = (Page)HttpContext.Current.Handler; 

    this.setCookieString(""); 
    myPage.Response.Write("done"); 
} 

并呼吁:

// Sets cookie date 
public void setCookieString(string cookiesData) 
{ 
    Page myPage = (Page)HttpContext.Current.Handler; 
    HttpCookie basketCookie = new HttpCookie("basket"); 
    basketCookie["items"] = cookiesData; 
    basketCookie.Expires = DateTime.Now.AddDays(7d); 
    myPage.Response.Cookies.Add(basketCookie); 
} 

我用在其他页面上的setcookiestring功能,它工作正常,但这种功能(从筐取出)没有设定饼干!它将“完成”写入页面的顶部,所以函数正在执行。

没有警告,没有错误,它只是不更新​​cookie。

+1

出于好奇,为什么要向HTTP响应添加一个值为“”的cookie? – RPM1984 2010-09-23 09:26:26

+0

这只是为了测试,我会把真实的数据放在里面,但我只是看到它是否将它设置为任何东西。无论我传入什么,cookie的值都不会改变。 – 2010-09-23 09:33:51

+0

也更容易以主页面的Response属性访问响应(就像在“Page”中一样),如果仅在(主)页面之外使用'HttpContext.Current.Response'。 – Richard 2010-09-23 09:34:21

回答

0

问题来自最初由Javascript设置的cookie,没有设置path=属性。 Javascript默认使用当前文件夹的cookie路径,而ASP.net默认为/

在Javascript设置cookie方法中设置path=/解决了此问题。