Whooo that is a humdinger. Normally, when someone says “I want my class to have access to the current session state!” I look at them funny, but here is the setup… I want to have global.asax set up some session goodies when someone hits my site, then I want all my web pages to work with those session variables in a standard way. The simple answer (whether you use the App_Code approach or deploy a compiled version of your web application) is to derive a class from the Page class like so:
public class CommonWebPage : System.Web.UI.Page
But you aren’t out of the woods – to truly do it, you’ll get barked at by VS2008 about implementing some required methods, and I found a post that explained some tips on the OnInit event. Also, since my global.asax was boxing up some goodies into session state, it ALLLLL ended up looking like this:
public class CommonWebPage : System.Web.UI.Page
{
//constructor ensures base Page() constructor is run
public CommonWebPage() : base() {}
//required overrides that anger vs2008
public override int GetTypeHashCode()
{
return base.GetTypeHashCode();
}
public override void ProcessRequest(HttpContext context)
{
base.ProcessRequest(context);
}
//the infamous OnInit event
protected override void OnInit(EventArgs e)
{
base.OnInit(e); //ensures Page.OnInit is called
//now, if you want to access Session state you can do it from here (if you try in the constructor it will be null)
System.Web.SessionState.HttpSessionState _Session = HttpContext.Current.Session;
//some handy debug code that dumps session state – compare it to you trace=”true” page directive
if (_Session == null)
{
HttpContext.Current.Response.Write(“Current Context returned a null session<br/>”);
}
else
{
HttpContext.Current.Response.Write(“r3kWebPage State…<br/>”);
for (int i = 0; i < _Session.Count; i++)
{
HttpContext.Current.Response.Write(“Session(” + _Session.Keys[i] + “)=” + Session[i].ToString() + “<br/>”);
}
}//end debug snippet
}//OnInit
}//class
Now, when I want to create a web page for my site, I use vs2008 Add Item and choose “Web Form” which wires up a lot of web goodness automagically. In this example I created a page called test.aspx – then, on the code-behind, I just change it to inherit from CommonWebPage instead of System.Web.UI.Page:
public partial class Test : CommonWebPage
OTHER NOTES:
- I saw a number of posts suggesting I set enableSessionState in the <page> directive of the site web.config, or to add <HttpModules> to it. I did NOT have to do any of that… and I wouldn’t recommend putting anything in web.config until yah HAVE to!
- I did have to elevate the trust on my application to full by putting <trust level=”Full” /> in the <system.web> section, but that is because my framework has a log file component that will be writing to the file system, and I was getting a security exception. Again, I wouldn’t throw full trust everywhere – just when you need it!
Enjoy the fun, and keep your eyes on www.therage3k.com – it is about to get a facelift 😉
/// Standard Page Functions for every web page in the site
/// </summary>
public class r3kWebPage : System.Web.UI.Page
{public AFrameFacade _AF { get; set; }
public AuthProvider _AUTH { get; set; }
public SimpleUser _USER { get; set; }
public r3kWebPage()
: base()
{
//_AF = _Session[“APP.AFRAME.FACADE”] as AFrameFacade;
//_AUTH = new AuthProvider(_AF);
//_USER = _Session[“APP.AFRAME.USER”] as SimpleUser;
//_AF.AddResult(new Result(“r3kWebPage”,”Constructor”,”Happy”, “hee hee hee”, ResultLevels.OK));
}
//REQUIRED OVERRIDES
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
System.Web.SessionState.HttpSessionState _Session = HttpContext.Current.Session;
if (_Session == null)
{
HttpContext.Current.Response.Write(“Current Context returned a null session<br/>”);
}
else
{
HttpContext.Current.Response.Write(“r3kWebPage State…<br/>”);
for (int i = 0; i < _Session.Count; i++)
{
HttpContext.Current.Response.Write(“Session(” + _Session.Keys[i] + “)=” + Session[i].ToString() + “<br/>”);
}
}
}
public override int GetTypeHashCode()
{
return base.GetTypeHashCode();
}
public override void ProcessRequest(HttpContext context)
{
base.ProcessRequest(context);
}