Solution to fail to obtain the value of context. session ["XXX"] On the ashx page

Source: Internet
Author: User

1. In aspx and Aspx. CS, both are read and write based on session ["XXX"] = "AAA" and AAA = session ["XXX"]. tostring. In ashx, sessions use context. Session, And the Read and Write methods remain unchanged.

 

2. in the ashx file, if you want to successfully read and write sessions, you should add the irequiressessionstate interface after using the session class (the namespace using system may be prompted when you add it. web. sessionstate reference), otherwise. sessio context N ["XXX"] always reads null

 

So, after the session can be used in aspx, aspx. CS, and ashx, how can we operate the session or obtain the visitor's IP address in the CS file of appcode?

First, aspx. CS directly requests and sessions, while ashx contains context. Request and context. Session. Aspx. CS can be used directly because of Web. UI support. In ashx, request and session operations can only be performed by passing in the httpcontext instance. Similarly, there is no web in the CS file in the appcode. UI, but there is no httpcontext parameter. If the current httpcontext can be obtained in CS, the request and session can be operated based on the httpcontext.

The httpcontext class has a static property called current. We can use this property to obtain the current httpcontext. Of course, the irequiressessionstate interface should also be added to operate sessions in CS.

 

Methods and attributes related to sessions, requests, response, and server can be applied according to this mode and method.

For example, to obtain the IP address of the current visitor in the CS file of appcode, you can use httpcontext. Current. Request. userhostaddress.

 

When creating an Ajax application, an ashx page is created to allow the front-end js to fl the page. In the public void processrequest (httpcontext context) method, context. session. sessionid and context. session ["userptid"] are used to record information. When I tested and checked the background data records, the two fields were empty. Why?

In fact, you only need to let your ashx page class inherit the system. Web. sessionstate. ireadonlysessionstate interface. This interface is a flag interface and does not have any method. It only allows the target HTTP handler to have read access to the value of the session status. For detailed usage, see msdn.

Solution to fail to obtain the value of context. session ["XXX"] On the ashx page

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.