如何从全局访问属性。asax在其他页面的代码后面

时间:2022-09-06 19:30:24

Imagine I have a property defined in global.asax.

假设我有一个定义在global。asax中的属性。

public List<string> Roles
{
    get
    {
        ...
    }
    set
    {
        ...
    }
}

I want to use the value in another page. how to I refer to it?

我想在另一个页面中使用这个值。我怎么说呢?

8 个解决方案

#1


17  

You can access the class like this:

您可以这样访问类:

((Global)this.Context.ApplicationInstance).Roles

#2


3  

It looks to me like that only depends on the session - so why not make it a pair of static methods which take the session as a parameter? Then you can pass in the value of the "Session" property from the page. (Anything which does have access to the HttpApplication can just reference its Session property, of course.)

在我看来,这只取决于会话——那么为什么不将它设置为一对静态方法,将会话作为参数呢?然后您可以从页面中传递“Session”属性的值。(当然,任何可以访问HttpApplication的东西都可以引用它的会话属性。)

#3


2  

If this is a property you need to access in all pages, you might be better defining a base page which all your other pages extend...

如果这是一个您需要在所有页面中访问的属性,那么您最好定义一个基础页面,所有其他页面都可以扩展这个基础页面……

e.g. by default

例如,在默认情况下

public partial class _Default : System.Web.UI.Page 
{
}

What you could do is add a BasePage.cs to your App_Code folder

你能做的就是添加一个BasePage。cs到App_Code文件夹。

public class BasePage : System.Web.UI.Page 
{
   public List<string> Roles
   {
       get { ... }
       set { ... }
   }
}

And then have your pages extend this.

然后让你的页面扩展这个。

public partial class _Default : BasePage
{
}

#4


1  

Hey, I'm popping my *.com cherry! My first answer after lurking for a month.

嘿,我要去*.com买樱桃!我的第一个回答潜伏了一个月。

To access a property defined in your Global class, use either of the following:

要访问全局类中定义的属性,请使用以下任一种方法:

  • the Application property defined in both the HttpApplication and Page classes (e.g. Page.Application["TestItem"])

    在HttpApplication和Page类中定义的应用程序属性(例如,Page.Application[" tem"])

  • the HttpContext.ApplicationInstance property (e.g. HttpContext.Current.ApplicationInstance)

    HttpContext。ApplicationInstance属性(例如HttpContext.Current.ApplicationInstance)

With either of these, you can cast the result to your Global type and access the property you need.

使用这两种方法,您都可以将结果转换为全局类型,并访问所需的属性。

#5


1  

You can also use the following syntax:

您还可以使用以下语法:

((Global)HttpContext.Current.ApplicationInstance).Roles

#6


0  

If the values are dependent on the Session then this is actually simple using the HttpContext.Items Dictionary:

如果值依赖于会话,那么使用HttpContext实际上很简单。字典项:

Place this code in the Global.asax to store the value:

将此代码放在全局中。asax存储该值:

Dim someValue As Integer = 5
Context.Items.Add("dataKey", someValue)

Let retreive it in a Page with this code:

让retreive在一个页面中使用以下代码:

Dim someValue As Integer = CType(HttpContext.Current.Items("dataKey"), Integer)

Here's a link that describes it in further detail: http://aspnet.4guysfromrolla.com/articles/060904-1.aspx

这里有一个更详细地描述它的链接:http://aspnet.4guysfromrolla.com/articles/060904-1.aspx

#7


0  

On the global.asax itself for .net 3.5 I used typeof(global_asax) and that worked fine. And, what actually lead me here was implementing the DotNet OpenID examples. I changed some of it to use the Application cache like Will suggested.

在全球。asax在。net 3.5中使用了typeof(global_asax),效果很好。实际上,在这里引导我的是实现DotNet OpenID示例。我将其中的一些更改为使用威尔建议的应用程序缓存。

#8


0  

For other layers of project which it is not possible to have Global as a class:

对于其他不可能作为类具有全局性的项目层:

dynamic roles= ((dynamic)System.Web.HttpContext.Current.ApplicationInstance).Roles;
if (roles!= null){
   // my codes
}

Just I have to be sure the Roles property in Global class never changes. and another way is by reflection.

只是我必须确保全局类中的role属性不会改变。另一种方法是通过反射。

#1


17  

You can access the class like this:

您可以这样访问类:

((Global)this.Context.ApplicationInstance).Roles

#2


3  

It looks to me like that only depends on the session - so why not make it a pair of static methods which take the session as a parameter? Then you can pass in the value of the "Session" property from the page. (Anything which does have access to the HttpApplication can just reference its Session property, of course.)

在我看来,这只取决于会话——那么为什么不将它设置为一对静态方法,将会话作为参数呢?然后您可以从页面中传递“Session”属性的值。(当然,任何可以访问HttpApplication的东西都可以引用它的会话属性。)

#3


2  

If this is a property you need to access in all pages, you might be better defining a base page which all your other pages extend...

如果这是一个您需要在所有页面中访问的属性,那么您最好定义一个基础页面,所有其他页面都可以扩展这个基础页面……

e.g. by default

例如,在默认情况下

public partial class _Default : System.Web.UI.Page 
{
}

What you could do is add a BasePage.cs to your App_Code folder

你能做的就是添加一个BasePage。cs到App_Code文件夹。

public class BasePage : System.Web.UI.Page 
{
   public List<string> Roles
   {
       get { ... }
       set { ... }
   }
}

And then have your pages extend this.

然后让你的页面扩展这个。

public partial class _Default : BasePage
{
}

#4


1  

Hey, I'm popping my *.com cherry! My first answer after lurking for a month.

嘿,我要去*.com买樱桃!我的第一个回答潜伏了一个月。

To access a property defined in your Global class, use either of the following:

要访问全局类中定义的属性,请使用以下任一种方法:

  • the Application property defined in both the HttpApplication and Page classes (e.g. Page.Application["TestItem"])

    在HttpApplication和Page类中定义的应用程序属性(例如,Page.Application[" tem"])

  • the HttpContext.ApplicationInstance property (e.g. HttpContext.Current.ApplicationInstance)

    HttpContext。ApplicationInstance属性(例如HttpContext.Current.ApplicationInstance)

With either of these, you can cast the result to your Global type and access the property you need.

使用这两种方法,您都可以将结果转换为全局类型,并访问所需的属性。

#5


1  

You can also use the following syntax:

您还可以使用以下语法:

((Global)HttpContext.Current.ApplicationInstance).Roles

#6


0  

If the values are dependent on the Session then this is actually simple using the HttpContext.Items Dictionary:

如果值依赖于会话,那么使用HttpContext实际上很简单。字典项:

Place this code in the Global.asax to store the value:

将此代码放在全局中。asax存储该值:

Dim someValue As Integer = 5
Context.Items.Add("dataKey", someValue)

Let retreive it in a Page with this code:

让retreive在一个页面中使用以下代码:

Dim someValue As Integer = CType(HttpContext.Current.Items("dataKey"), Integer)

Here's a link that describes it in further detail: http://aspnet.4guysfromrolla.com/articles/060904-1.aspx

这里有一个更详细地描述它的链接:http://aspnet.4guysfromrolla.com/articles/060904-1.aspx

#7


0  

On the global.asax itself for .net 3.5 I used typeof(global_asax) and that worked fine. And, what actually lead me here was implementing the DotNet OpenID examples. I changed some of it to use the Application cache like Will suggested.

在全球。asax在。net 3.5中使用了typeof(global_asax),效果很好。实际上,在这里引导我的是实现DotNet OpenID示例。我将其中的一些更改为使用威尔建议的应用程序缓存。

#8


0  

For other layers of project which it is not possible to have Global as a class:

对于其他不可能作为类具有全局性的项目层:

dynamic roles= ((dynamic)System.Web.HttpContext.Current.ApplicationInstance).Roles;
if (roles!= null){
   // my codes
}

Just I have to be sure the Roles property in Global class never changes. and another way is by reflection.

只是我必须确保全局类中的role属性不会改变。另一种方法是通过反射。