ASP.net 母版页中的状态
我们有一个错误正在尝试修复。看起来一个用户可以看到另一个用户同时访问同一个 aspx 页面的数据。
我想知道:如果我们将数据存储在母版页上的属性中,那么同时运行的所有页面都会看到该数据。
有一个母版页:
public class MyMasterBase : System.Web.UI.MasterPage
{
private int myInt;
}
从 aspx 页面创建对母版页的引用:
MyMasterBase master = Page.Master as MyMasterBase;
然后两个用户同时调用 aspx 页面:
- 第一个用户将 myInt 设置为 1
- 第二个用户将 myInt 设置为 2
- 如果第一个用户读取 myInt 值会发生什么会吗?
我预计它会是 1,但如果它是 2,那就可以解释我们的错误了:)
谢谢
Shiraz
We have a bug that we are trying to fix. It looks like one user can see another users data it they hit the same aspx page at the same time.
I was wondering: If we store data in a property on the master page, will all pages running at the same time see that data.
Have a master page:
public class MyMasterBase : System.Web.UI.MasterPage
{
private int myInt;
}
Create reference to master page from the aspx page:
MyMasterBase master = Page.Master as MyMasterBase;
Two users then call the aspx page at the same time:
- The first user sets myInt to 1
- The second user sets myInt to 2
- If the first user reads the myInt value what will it be?
I expect that it would be 1, but if it was 2 it would explain our bug :)
Thanks
Shiraz
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对我来说听起来像是缓存问题。 MasterPage 不会在会话之间保留数据,但缓存会。查找项目中将信息放入缓存的所有位置,并确保没有用户或会话特定的数据进入其中。缓存应该只保存在给定时间适用于所有用户的信息。
Sounds like a cache problem to me. MasterPages don't persist data between sessions, but the Cache will. Find everywhere in your project that you're putting information into the Cache and ensure no user or session-specific data is going into it. Cache should only hold information that applies to all users at a given time.
这取决于您使用
myInt
执行的操作。如果您将其值存储在会话中,请检查您的用户身份(Page.User.Identity.Name
),如果它们相等,您将读取最新的设置值。如果将其存储在 ViewState 中,每个用户都会读取自己的值。如果您根本不保留它(我假设是这种情况),则该值将不会被保存(根据每个用户的每个请求重置)。也许你想要的是这样的(只是猜测):That depends on what you do with
myInt
. If you store its value in Session check your users' identities (Page.User.Identity.Name
), if they're equal you will read the latest set value. If you store it in ViewState each user will read his own value. if you don't persist it at all (i assume that is the case), the value will not be saved (reset on each request for each user). Probably what you want is something like this (just a guess):