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