English 中文(简体)
State in ASP.net Master Page
原标题:

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

最佳回答

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.

问题回答

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):

int property MyInt {
    get { return Convert.ToInt32(ViewState["MyInt"]);}
    set { ViewState["MyInt"] = value; }
}




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签