views:

1978

answers:

6

Imagine I have a property defined in global.asax.

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

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

+2  A: 

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

Jon Skeet
Sorry, I downvoted your post. As I've been thinking about it you are totally right. I want to switch my vote to an upvote but stackoverflow won't let me anymore. Please accept my thanks instead.
minty
@minty: Just click on the highlighted down vote and it will undo it. That may enable to you upvote afterwards.
Jon Skeet
A: 

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

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
{
}
Eoin Campbell
How would this affect the visual studio designer etc?
minty
+4  A: 

You can access the class like this:

((Global)this.Context.ApplicationInstance).Roles
Panos
This idea worked for me but I had to modify your example slightly: ((global_asax)Context.ApplicationInstance).RolesFor some reason in my project the global.asax item had type global_asax.
minty
It took me some time to understand why this didn't work for properties which are intended to live for the whole application time. For example: If you create "Roles" in the example above in "Application_Start" then using ApplicationInstance to access the "Roles" property can fail because there isn't only one "global application instance" for every request. To have globally existing properties in global.asax those properties must be static. The following blog post helped me a lot to understand what's going on: http://odetocode.com/articles/89.aspx
Slauma
A: 

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

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

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

Chris Pietschmann
+1  A: 

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

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"])

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

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

A: 

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.

Marc