How to get the Session value on the ashx page and the session on the ashx page
On the general transaction processing page, you can easily obtain the Request and Response objects and perform the following operations:
HttpRequest Request = context. Request;
HttpResponse Response = context. Response;
However, it is not that easy to obtain the Session value. For example, you need to obtain the Logon account Session ["userAccount"] stored in the Session in ashx.
If you only use context. Session ["userAccount"], an exception will be reported "the object has not been referenced to the instance of the object ".
Therefore, if you want to obtain the value in the Session, you need
1. Introduce the namespace:
Using System. Web. SessionState;
2. Implement the IRequiresSessionState interface as follows:
/// <Summary>
/// $ Codebehindclassname $ abstract description
/// </Summary>
[WebService (Namespace = "http://tempuri.org/")]
[WebServiceBinding (ConformsTo = WsiProfiles. BasicProfile1_1)]
Public class AddUserInfo: IHttpHandler, IRequiresSessionState // This is the implementation of the display. No implementation method is required.
{
Public void ProcessRequest (HttpContext context)
{
//...
// You can perform the following operations:
If (context. Session ["userAccount"]! = Null)
{
String account = context. Session ["userAccount"]. ToString ();
}
//... Continue the following code
}
}
How does ashx store sessions? How does it get them in aspxcs?
On the front-end page, you can use Session ["TimeName"] to obtain your value. However, you cannot directly use session in ashx, you must first add an interface and a namespace reference. Using System. Web. SessionState; then implement your IRequiresSessionState interface. Otherwise, an exception will be thrown on the aspx page. Example: public class Test: IHttpHandler, IRequiresSessionState
How does ashx store sessions? How does it get them in aspxcs?
The same is true for normal sessions. There is nothing special about it. If you call it, you can directly use the CS file, which is of the same nature as aspx. cs.