Generally, session is used to determine whether a user is logged on. Some temporary and important data is also stored in the session. On the page, we can easily get the session value, but some problems will occur in the class. You can also obtain it through the following method. System. Web. httpcontext. Current. session [ " Userinfo " ]; But today, this method also fails. To create a small application, you must implement ihttphandler and use the user ID. However, the session value cannot be found in this class. system. web. httpcontext. current. the session is null Why is the obtained session null? I haven't figured it out for a long time. After looking for a long time, I found the expert's guidance and solved the problem. solution: while implementing ihttphandler, you must also implement the irequiressessionstate interface, its namespace is: system. web. sessionstate. Public class watermarkhandler: ihttphandler, system. web. sessionstate. irequiressessionstate {} continue tracing: Why is this interface implemented? What is the purpose of this interface? Continue tracing. msdn gave the final explanation. irequiressessionstate specifies the target HTTP processing Program . read/write access to the session status value. This is a tag interface and there is no way. function: implement the irequiressessionstate interface in a custom HTTP handler, to determine whether the handler needs to have read and write access to the session status value . So remember, if you want to access the session in a custom HTTP handler, remember to implement this interface. Remember !!! |