When you want to access a session from an ashx file you will find that using context.Session[“value”] , will not allow you to access the session variables from the previous file where the session was available from. This is expected behavior and based on some guides Ihave seen, is often over complicated. Rather than building a work around that will pass the values through the URL or some other method, it is actually quite easy to just gain access to this session value again from the ashx file by implementing a single class.
To implement another class in your current class you need to alter the class declaration. By altering the class declaration and implementing the IRequiresSessionState class, you will be able to access a session information from an ashx file, without needing to built a messy work around that requires data to be sent through a different, less convenient medium. The class you need to implement is “System.Web.SessionState.IRequiresSessionState”. The class declaration should look something like this.
public class MyHandler : IHttpHandler, System.Web.SessionState.IRequiresSessionState { string myval = HttpContext.Current.Session["myval"] }
This is all you need to access a session from an ashx file. You dont need to be writing anything to a particular session file or creating any fancy methods. Using this method is all you need. This will save you the trouble of building a messy work around.