I need to be able to change when I can see session state. I found out about the IRequiresSessionState
Marker Interface, but have not been able to figure out how to use it. I think I may be missing something obvious. Can one of you C# guru's give me a quick 1-2-3 step through (e.g)?
public interface IRequiresSessionState
in it. IRequiresSessionState('abra_cadabra')
to change the value.public class PageTools
Marked, it would look like public class PageTools : IRequiresSessionState
or with both interfaces, public class PageTools : IRequiresSessionState, IHttpHandler
. In my case, my class needed only to be marked with the first. My handler needed both.implement
from the menu (visual studio) and the necessary methods will be added to your class. Or you can look them up and add them manually.Once you have the IRequiresSessionState marker you can test to see if the session state is readonly and if so set a new http handler.
if (context.Handler is IReadOnlySessionState
|| context.Handler is IRequiresSessionState)
{
context.Handler = Handler();
}
The http handler: MSDN will tell you a lot about HttpHandlers and HttpModules. In my case I needed a dummy handler so that I could access the session state when it was normally read only (Page_PreInit
). So in my class I added this:
protected IHttpHandler Handler()
{
MyHttpHandler resourceHttpHandler = HttpContext.Current.Handler
as MyHttpHandler;
if (resourceHttpHandler != null) // set the original handler back
{
return resourceHttpHandler.OriginalHandler;
}
// at this point session state should be available
return HttpContext.Current.Handler;
}
public class MyHttpHandler : IHttpHandler, IRequiresSessionState
{
internal readonly IHttpHandler OriginalHandler;
public MyHttpHandler(IHttpHandler originalHandler)
{
OriginalHandler = originalHandler;
}
public void ProcessRequest(HttpContext context)
{
// do not worry, ProcessRequest() will not be called,
// but let's be safe
throw new InvalidOperationException(
"MyHttpHandler cannot process requests.");
}
public bool IsReusable
{
// IsReusable must be set to false since class has a member!
get { return false; }
}
}
Here is a reference to a very elegant HttpModule class from which I got much or what I used. I hope this helps someone.
You have just to derive your HTTP Handler class from IRequiresSessionState to get SessionState access.
public class MyHttpHandler : IHttpHandler, IRequiresSessionState
{
public void ProcessRequest(HttpContext context)
{
var MyValue = context.Session["MyKey"] as String;
MyValue = "Hello World";
context.Session["MyKey"] = MyValue;
}
public bool IsReusable
{
get { return true; }
}
}
The IRequiresSessionState
interface is a marker interface and contains no methods, so you can't use it the way you are asking in the question. You only implement the interface in an HTTP handler to identify that the handler requires read and write access to session-state values.
To see how you implement it in an HTTP handler, check out this link: http://www.willasrari.com/blog/irequiressessionstate-and-dynamic-data-in-static-pages/000262.aspx
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With