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?
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?
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.)
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
{
}
You can access the class like this:
((Global)this.Context.ApplicationInstance).Roles
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
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.
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.