我的 session 类中有这个字段:
public bool IsCartRecentlyUpdated
{
get
{
if (this.session["IsCartRecentlyUpdated"] != null)
{
return (bool)this.session["IsCartRecentlyUpdated"];
}
else
{
this.session["IsCartRecentlyUpdated"] = false;
return (bool)this.session["IsCartRecentlyUpdated"];
}
}
set
{
this.session["IsCartRecentlyUpdated"] = value;
}
}
每当用户将产品添加到购物车时,我都会将此值设置为 true:
public void AddToCart(Product product, int quantity)
{
IsCartRecentlyUpdated = true;
//other code for updating the cart
}
将产品添加到购物车会进行回发,因此我可以在购物车所在的通用母版页的 Page_Load 中显示消息(例如:产品已成功添加)位于,当产品刚刚添加到购物车时:
protected void Page_Load(object sender, EventArgs e)
{
if (this.sessionsUtil.IsCartRecentlyUpdated)
{
this.lblCartWarning.Text = (string)GetLocalResourceObject("CartWarning");
imgCardLogos.Visible = false;
}
else
{
this.lblCartWarning.Text = String.Empty;
imgCardLogos.Visible = true;
}
//other code
//put it back to false to not show the message each time the page is loaded
this.sessionsUtil.IsCartRecentlyUpdated = false;
}
好吧,这段代码在本地很好地工作,但是在服务器上,它在添加产品添加到购物车但在第二页加载... (我猜想在服务器上以某种方式在更新 session 变量之前加载页面 - 非常奇怪)
你知道为什么吗?我没有在代码中看到任何问题...
最佳答案
像这样的奇怪问题使用 IIS express 可能更容易解决 http://weblogs.asp.net/scottgu/archive/2011/01/03/vs-2010-sp1-beta-and-iis-developer-express.aspx
关于c# - 奇怪的 session 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4547747/