开发者

asp.net system variable?

Is it possible to set a variable in the system which is not for each user unique?, who access the page?

Ex.

I access the page and in codebehind something like that:

// creat开发者_JAVA技巧e variable over all
if (sysstring != null || "")
SystemString sysstring = DateTime.now;

So if another user already accessed the page, I receive the value of the date when he accessed the page.

Thank you


You're looking for Application scope:

string lastAccess = (DateTime)Application["lastAccess"];

Altho this will reset with every app recycle. I would suggest storing it in a DB, which is where all cross-user variables should be!


You can use the Application object:

HttpApplicationState app = this.Context.Application;
DateTime myValue = null;
app.Lock();
try
{
    myValue = (DateTime)app["key"];
    if (myValue == null)
    {
        myValue = DateTime.Now;
        app["key"] = myValue;
    }
}
finally
{
    app.UnLock();
}


Why not just make this static?

static string sysstring;

if (string.IsNullOrEmpty(sysstring)) sysstring = DateTime.Now;

As 'Loren said, just about anything other than storing this in the database will be lost when the app recycles.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜