An error occurred while obtaining the session value.

Source: Internet
Author: User

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. Session is null

 

Why is the obtained session empty? 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

Specify the target HTTP handler to have read and write access to the session status value. This is a tag interface and there is no way.

Purpose:

Implement the irequiressessionstate interface in the 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, you must implement this interface.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.