C# 如何在后面的其他页面代码中访问 global.asax 中的属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/251987/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 20:00:39  来源:igfitidea点击:

How do I access properties from global.asax in some other page's code behind

c#asp.netglobal-asax

提问by minty

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?

我想在另一个页面中使用该值。我该如何参考呢?

采纳答案by Panos

You can access the class like this:

您可以像这样访问该类:

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

回答by Jon Skeet

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 doeshave access to the HttpApplication can just reference its Session property, of course.)

在我看来,这仅取决于会话 - 那么为什么不让它成为一对将会话作为参数的静态方法呢?然后您可以从页面传入“Session”属性的值。(任何有确实有权访问的HttpApplication可以直接引用其Session属性,当然)。

回答by Eoin Campbell

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
{
}

回答by Chris Pietschmann

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

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

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

将此代码放在 Global.asax 中以存储值:

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

Let retreive it in a Page with this code:

让我们使用以下代码在页面中检索它:

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

回答by Chris Pietschmann

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

嘿,我正在弹出我的 stackoverflow.com 樱桃!潜伏一个月后我的第一个答案。

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

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

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

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

  • 在 HttpApplication 和 Page 类中定义的 Application 属性(例如 Page.Application["TestItem"])

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

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

使用其中任何一个,您都可以将结果转换为您的 Global 类型并访问您需要的属性。

回答by Marc

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.

在 .net 3.5 的 global.asax 本身上,我使用了 typeof(global_asax) 并且效果很好。而且,真正引导我来到这里的是实现 DotNet OpenID 示例。我更改了其中的一些以使用 Will 建议的应用程序缓存。

回答by Meysam

You can also use the following syntax:

您还可以使用以下语法:

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

回答by Daniel B

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

对于不可能将 Global 作为类的其他项目层:

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.

只是我必须确保 Global 类中的 Roles 属性永远不会改变。另一种方法是通过反射。