minty
minty

Reputation: 22421

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

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?

Upvotes: 14

Views: 27356

Answers (8)

Daniel B
Daniel B

Reputation: 3185

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.

Upvotes: 0

Meysam
Meysam

Reputation: 18137

You can also use the following syntax:

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

Upvotes: 1

Marc
Marc

Reputation: 1178

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.

Upvotes: 0

Will
Will

Reputation:

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.

Upvotes: 1

Chris Pietschmann
Chris Pietschmann

Reputation: 29895

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: https://web.archive.org/web/20210608183011/http://aspnet.4guysfromrolla.com/articles/060904-1.aspx

Upvotes: 0

Panos
Panos

Reputation: 19142

You can access the class like this:

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

Upvotes: 19

Eoin Campbell
Eoin Campbell

Reputation: 44268

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

Upvotes: 2

Jon Skeet
Jon Skeet

Reputation: 1500525

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

Upvotes: 3

Related Questions