不同场景下ASP页面上的静态变量 [英] Static variables on ASP pages in different scenarios

查看:104
本文介绍了不同场景下ASP页面上的静态变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道你是否可以在IIS 6服务器上确认某些静态变量模式。

案例1非常清楚。

但是案例2呢? br />
我的观察是否正确?



案例1

当声明为 ASPX脚本的一部分时页面例如:

< script runat =server>



static string verfilter =;

静态字符串APIfilter =;



...其他一些代码



页面被视为一种多访问方法。

当一些用户同时访问该页面时,每个用户都会收到此页面的任何用户刚刚建立的静态变量的值(最后更改状态)



案例2

在代码后面声明(在单独的* .cs文件中属于特定ASPX 文件),例如:



公共部分类TestClass:System.Web.UI.Page

{

静态的 int intEditor;

静态字符串strName;



...其他一些代码

}



看起来页面背后的代码为打开页面的每个Web用户创建了一个单独的TestClass对象实例。

看起来像是很少有用户同时访问该页面,每个用户都会收到他自己的一组静态变量。

由一个Web用户完成的更改不会影响其他Web用户。

解决方案

静态字段将始终引用相同的内存位置(在同一AppDomain中),无论该类的哪个实例使用它。这意味着TestClass的所有实例都具有相同的intEditor和strName。代码所在的文件并不重要,在编译过程结束时,它们都是相同的MSIL。



如果需要每个实例变量,使用标准属性。



看看:

http://msdn.microsoft.com/en-us/library/aa645629(v = vs.71).aspx [ ^ ]

I would like to know if you could confirm certain static variables pattern on IIS 6 server.
Case 1 is quite clear.
But what about case 2?
Is my observation correct?

Case 1
When declared as part of a script of ASPX page e.g.:
<script runat="server">

static string verfilter = " ";
static string APIfilter = " ";

…some other code

The page is considered as a multi access method.
When a few users access the page simultaneously each user receives the value of static variables established a moment ago by any user of this page (last change state)

Case 2
When declared in code behind (in a separate *.cs file that belongs to a specific ASPX file), e.g.:

public partial class TestClass : System.Web.UI.Page
{
static int intEditor;
static string strName;

…some other code
}

It looks like the code behind of the page creates a separate object instance of the TestClass for each web user that opens the page.
It looks like that when a few users access the page simultaneously each user receives his own set of static variables.
Changes done by one web user do not affect other web users.

解决方案

A static field will always refer to the same memory location (in the same AppDomain), regardless of which instance of the class uses it. This will mean that all instances of TestClass have the same intEditor and strName. It doesn't matter what file the code sits in, at the end of the compilation process it's all the same MSIL.

If you need a per-instance variable, use standard properties.

Have a look at:
http://msdn.microsoft.com/en-us/library/aa645629(v=vs.71).aspx[^]


这篇关于不同场景下ASP页面上的静态变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆