Asp. NET custom classes use session to save information

Source: Internet
Author: User
Tags httpcontext


The session is a built-in object for the page class and can be used directly, but ordinary classes, such as generic processing classes, cannot be used directly.
I used a similar system.web.httpcontext.current.session["userid" in the handler class before, but the debug display
System.Web.HttpContext.Current.Session is always null.

workaround : Let the custom class implement the IRequiresSessionState interface while adding: using System.Web.SessionState;
Why do you want to implement this interface?

Here to illustrate the role of the IRequiresSessionState interface:

IRequiresSessionState: Specifies that the destination HTTP handler requires read and write access to the session-state value. This is a markup interface and there is no method.

Purpose: Implements the IRequiresSessionState interface in a custom HTTP handler to determine whether the handler needs read and write access to the session-state value.

So remember, if you want to access the session in a custom HTTP handler, be sure to implement this interface.

There is another interface, Ireadonlysessionstate, which allows only read-only operations on the session, and in addition, as with the IRequiresSessionState function.

Asp. NET custom classes use session to save information

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.